I have a child window in Silverlight and I wish to send a string value to populate a text box in the applications MainPage.xaml.
How can I pass the value back?
I have tried this -
MainPage m = (MainPage)Application.Current.RootVisual;
m.textBox1.Text = value;
You should do this the other way around. The parent that opens the child window should attach an event handler to an event of the child, for example:
childwindow.ButtonClicked += new EventHandler(childWindow_ButtonClicked);
Within this handler, the Parent can update its own Controls with values from properties of the child Window.
private void childWindow_ButtonClicked(object sender, EventArgs e)
{
txtValue.Text = childwindow.Value;
}
Assuming that you are using the mvvm pattern, you could use the ShowDialog method of the child window to open it.
The ShowDialog method waits until the window is closed.
After the window is closed, you can read the dependent properties from the window viewmodel and set their values in the mainpage.
var view = new ChildWindowView();
var model = new ChildWindowViewModel();
view.DataContext = model;
var result = view.ShowDialog();
Related
I have a parent UserControl that has a Button. On button_click a child UserControl is added into Parent UserControl. That newly added Child UserControl have a TextBox.
Now, Parent UserControl has another Button when clicked i want to get the value of TextBox of child UserControl. How can i get that ?
Plus, let's say the child UserControl added on RunTime are more than 1. Then how can i get TextBox value of all of these child UserControls ?
EDIT and UPDATE !
Child UserControl have this method
public string GetText()
{
return ProductNameBox.Text;
}
Parent UserControl have this
public List<UserControl> UserControlList = new List<UserControl>();
public void NewProductModule(object sender, RoutedEventArgs e)
{
AddProductModule productModules = new AddProductModule();
UserControlList.Add(productModules);
}
And This method
private void PreviewPdfFunc(object sender, MouseButtonEventArgs e)
{
foreach (UserControl cnt in UserControlList)
{
MessageBox.Show(cnt +" Total = " + StackPanelContainer.Children.Count);
}
}
First of, add every AddProductModule you create to a list (because you have to store them somewhere)
List<AddProductModule> AllControls = new List<AddProductModule>();
AllControls.Add(YourItem);
Second, you have two Options to get the text
First: Set the button to public
Second: Create a public method which you can call from your Parent, for example
public string GetTBText()
{
return TextBoxExample.Text;
}
In the end, to get all Texts, you could
foreach(AddProductModule item in AllControls)
{
string ValueOfTB = item.GetTBText();
}
EDIT
There was the Problem, that a wrong object type was in use, which made Problems with my answer. Formerly, the type was not AddProductModule, instead it was UserControl, and before that it was var (which was the main Problem to begin with)
If you come here, you should now be able to run the code in my answer without a problem
in my application i want to implement an options dialog like you have in VisualStudios if you go to Tools->Options in the menubar. How can i do this? My first idea was to use pages and navigation but maybe there's an easier approach?
It's probably not the easiest way but I wrote this snippet that match your goal and it's a good exercise.
In an empty Windows Forms project add a ListBox (listBox1) and a Panel (panel1). Then create 2 UserControls (UserControl1 and UserControl2), these will be the content that is shown when you click the list.
In your Form1 class we create a ListItem class that will contain your menu options as such:
public partial class Form1 : Form
{
public class ListItem
{
public string Text { get; set; }
public UserControl Value { get; set; }
public ListItem(string text, UserControl value)
{
Text = text;
Value = value;
}
};
...
}
After that you add items to the ListBox right after InitializeComponent() in Form1:
public Form1()
{
InitializeComponent();
listBox1.DisplayMember = "Text";
listBox1.ValueMember = "Value";
listBox1.Items.Add(new ListItem("Item1", new UserControl1()));
listBox1.Items.Add(new ListItem("Item2", new UserControl2()));
}
This will make it so when you use listBox1.SelectedItem it will return an object that you can cast to a ListItem and access the associated UserControl.
To make use of this behaviour, go to designmode and double-click the ListBox, this'll add code for the SelectedIndexChanged event. We use this event to display the UserControl in the Panel panel1. This will clear any old Panel content and add a selected UserControl:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
panel1.Controls.Clear();
UserControl control = (listBox1.SelectedItem as ListItem).Value;
if(control != null)
{
panel1.Controls.Add(control);
control.Dock = DockStyle.Fill;
}
}
I suggest you try adding a button or something to differentiate the UserControls and play around. Have fun! :)
You should create a new Window and show that as opposed to create a page and navigate to it. Then you would call .show() on the new window for it to show.
Then you would change the look of the new window to however you want, the same as editing pages.
If you build your options into a full object model that matches the structure of the options window, then the best way is to use whatever navigation-aware UI binding that your MVVM toolkit uses. The options window would start off as a new root level window to which you would bind the root of your options data model.
So, in short think of the options dialog as a mini-application that uses the same structure as your main MVVM application, but with a different data model root.
If you plan to allow the user to cancel the changes to the options, then you would want your options data model to be clonable so that you can populate the options window with the clone and then swap out the real options with the new data if the user presses OK on the options window. If they select cancel you can just throw the cloned object away and destroy the window.
I have a user control with a button which when clicked opens a new user control.
private void Button_Click(object sender, RoutedEventArgs e)
{
Window window = new Window
{
Title = "Window2",
Content = new UserDataControl2()
};
window.ShowDialog();
}
I need to pass a collection to the new user control. How can I do it?
The easiest way is to create a custom constructor for your user control.
// Button_Click event
Window window = new Window
{
Title = "Window2",
Content = new UserDataControl2("My Data");
};
// User Control class.
string _info;
public UserDataControl2(string info)
{
_info = info.
};
You could also create a method or property in the user control to receive the data as well. Use whichever seems more appropriate in your context.
The best way is passing object to DataContext of this Window. For this you will need to create a class where store this parameters (ViewModels) and after "binding" to the Window (View). After you can pass this object assigning to Datacontext.
Look to MVVM model to understand better what I mean.
MVVM Pattern Made Simple
MVVM in Depth
Close child Window after clicking ok/cancel.
I am invoking the child window from parent mvm model:
//parent view model
var optionSetViewModel = new OptionSetViewModel();
var optionSet = new OptionSet();
optionSet.SetViewModel(optionSetViewModel);
optionSet.SetOwner(_componentview);
optionSet.ShowDialog();
The code for ok and cancel for child Window is written in child view model:
private void OkClick()
{
_childWindow.Close();
}
It closing fine but after closing when it is returing to parent view model from where it is called it is throwing null reference exception. After close I am accesing the child window values:
_someText = optionSetViewModel.SomeText;
_noteText = optionSetViewModel.NoteText;
_optionsetLanguage = optionSetViewModel.OptionSetSelectedItem;
_optionsetselected = optionSetViewModel.OptionSetSelected.ToString();
With Catel (see http://www.catelproject.com/) you can close a view based on a view model. Thus closing a view means a call like this:
myChildViewModel.CloseViewModel();
To show a child window:
var myChildViewModel = new MyChildViewModel();
uiVisualizerService.Show(myChildViewModel);
How we can pass the values from a child window to the parent window in silverlight 3 by using Properties (ie Databinding).
In my work, it includes a parent window and a child window. The child window contains a text box, which returns a value after the hitting OK button it to Parent window. I already did this by using message sending and receiving events. But now i wish to change that to using properties.
Parent Window:
ChildWindow myWin = new MyWindow("Test", "Test of shared ui elemnts");
myWin.Show();
myWin.Closed += new EventHandler(myWin_Closed);
void errorWin_Closed(object sender, EventArgs e)
{
ErrorWindow wrr = (ErrorWindow)sender;
string mytext = wrr.MyText; // Can access any property that was set ChildWindow
}