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
Related
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.
In my current project, I have multiple forms for 6 different types of Promotion. My current UI design is to pop up a mini form on run which allows the users to select promotion type and direct them to each form based on the selection.
Instead, I'd like to use TreeView control like Windows Explorer left pane and group them under one main root so that I'll not need to create multiple forms for each type of promotion.
Layout will be like this:
My question is how can I manage controls from different forms?
If I put all the controls together under one form and show/hide on NodeMouseClick Event, my design view will be very messy.
In tab control, I can manage a related set of controls under each tab.
But I think that using tab control for each type of promotion is not the right way to do it.
Any help or suggestion will be very much appreciated!
The easiest way to do this would be to convert your application to MDI so you can reuse your existing forms.
If the controls on each form are the same but with different values you can easily use one form and a list of a collection of values that you can plug the values from the appropriate collection into the form. If the controls will be different, design different panels or groupboxes that you can store in memory and add to the form as needed.
A possible approach could be to work with user controls.
Something like this should work
private UCDummy1 ucDummy1;
private void TreeView_Loaded(object sender, RoutedEventArgs e)
{
var item = new TreeViewItem();
item.Header = "Options";
item.ItemsSource = new string[] {"Dummy1", "Dummy2", "Dummy3"};
var tree = sender as TreeView;
tree.Items.Add(item);
PopulateConfigUserControls();
}
private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
var tree = sender as TreeView;
if (tree.SelectedItem is string)
{
string selectedUC = tree.SelectedItem.ToString();
PanelOption.Children.Clear();
if (selectedUC == "Dummy1")
{
PanelOption.Children.Add(ucDummy1);
}
}
}
private void PopulateConfigUserControls()
{
ucDummy1 = new UCDummy1();
ucDummy1.TextBoxVoornaam.Text = Settings.Default.Voornaam;
ucDummy1.TextBoxAchternaam.Text = Settings.Default.Achternaam;
}
private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
Settings.Default.Voornaam = ucDummy1.TextBoxVoornaam.Text;
Settings.Default.Achternaam = ucDummy1.TextBoxAchternaam.Text;
Settings.Default.Save();
}
I create a user control and add a textbox to it. In my windows form I add the user control i created and add a textbox and a button. How to copy the text I input from the textbox of Form to textbox of Usercontrol and vice versa. Something like
usercontrol.textBox1.text = textBox1.text
You could add to your User Control code a public property that delegates into the TextBox's Text property:
public string MyTxtBoxValue { get { return this.txtBox.Text; } }
And you could also have a setter to that, of course, if needed.
What you don't want to do, however, is exposing the whole TextBox by making it public. That is flawed.
From Form to Usercontrol
Form Code
public string ID
{
get { return textBox1.Text; }
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
userControl11.ID = ID;
}
Usercontrol Code
public string ID
{
set { textBox1.Text = value; }
}
There are multiple ways to access your user control text box data. One way to accomplish this would be to expose the text box on the user control at a scope that can be accessed via the form it's loaded on. Another way would be raising an event on the button click of the user control and subscribing to it on the parent form.
Although some stuff are inherited when creating a custom user control, for the most part you have to define your own properties. (like text value, etc..)
I would take a look at this:
http://msdn.microsoft.com/en-us/library/6hws6h2t.aspx
good luck!
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();
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
}