How to switch between user controls, in a given window area
I'm trying to write an app where the left side of the app window is a tree menu, and the right side is a user control depending on the menu selection.
I've drawn a border around the right side, and when the user selects an item in the menu, I set the Child property of the border to be the user control corresponding to that menu item, like so:
Foo foo = new Foo(); // is a UserControl descendant
Bar bar = new Bar();
private void Foo_Selected(object s开发者_如何转开发ender, RoutedEventArgs e)
{
RightSideBorder.Child = foo;
}
private void Bar_Selected(object sender, RoutedEventArgs e)
{
RightSideBorder.Child = bar;
}
The problem is, that whenever have bar
selected and select foo
, bar
gets an Unloaded
event (and vice versa).
What I want to do, is to keep foo and bar loaded, and just switch between them depending on the menu selection. How could I accomplish this? Changing the Border.Child property is obviously the wrong way of doing this.
Put all the controls in a single cell grid, and only change their Visibility property to Collapsed / Visible.
精彩评论