public partial class Window1 : Window { public Window1() { InitializeComponent(); }
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
switch ((sender as Button).Content.ToString())
{
case "UserControl 1":
AddItemToContainer(new UserControl1());
break;
case "UserControl 2":
开发者_C百科AddItemToContainer(new UserControl2());
break;
case "UserControl 3":
AddItemToContainer(new UserControl3());
break;
default:
break;
}
}
void AddItemToContainer(UIElement _myElement)
{
Grid.SetColumn(_myElement, 1);
HostContainer.Children.Add(_myElement);
}
}
}
}
With this I can open a new userControl in myMainwindow
Let’s say something like adding child to myMainWinodw,Now I’m trying to click on a button from my userControl so I open another userControl that take the place of the first one
I explain:
I have the mainWindows it has 3 button first one to open the first UserControl the second one to open the second userControl and the third to open the last UserControl,imagine that I opened the first UserControl let’s call it UC1,
In the UC1 I have a button to open the second userControl (let’s call it UC2) I like that when I clik the button from the UC1 the UC2 is opened and take the place of the UC1 (of course the UC2 is still a child of myMainWinodw) I have alredy try to call the AddItemToContainer methode from other methode but nothing is happened
Any suggestion please
This approach may help:
Call the container control and modify it's Children.This example assumes that the container control is a Grid.
C# Code for Window1
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
HostContainer.Children.Add(new UserControl1(HostContainer));
}
}
C# Code for UserControl1
public partial class UserControl1 : UserControl
{
Grid _hostContainer;
public UserControl1(Grid HostContainer)
{
InitializeComponent();
_hostContainer = HostContainer;
}
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
{
UserControl2 UC2 = new UserControl2();
_hostContainer.Children.Add(UC2);
}
}
Yes that helps but it does not realy take the place of the UC1 It just puch it a litel bit and take a litelle space of the mainWindow here is some snaps with explanation if you like http://startou.com/file/630-9411954b18.html
精彩评论