Reputation: 13
I try to switch usercontrol using another usercontol button and the second usercontol is set on main window of wpf application,
for example
use user_list
usercontrol button and switch add_user
user control this usercontrol(add_user usercontrol) set on the main window
user_list.xaml
Upvotes: 0
Views: 776
Reputation: 4933
You have a lot of ways to achieve this without using additional framework:
add_user
and user_list
in your MainWindow.xaml
like this:<Grid>
<local:user_list x:Name="userList"></local:user_list>
<local:add_user x:Name="addUser" Visibility="Hidden"></local:add_user>
</Grid>
And in your click event handler in your code-behind, switch the Visibility
properties:
private void Button_Click(object sender, EventArgs e) {
userList.Visibility = Visibility.Hidden;
addUser.Visibility = Visibility.Visible;
//Do other stuff like update data if needed
}
UserControl
user_list
in your MainWindow
like this:<Grid>
<local:user_list x:Name="userList"></local:user_list>
</Grid>
And create and display a new Window
to display your add_user
control in the code-behind:
private void Button_Click(object sender, EventArgs e) {
var window = new Window();
window.SizeToContent = SizeToContent.WidthAndHeight;
window.Content = new add_user();
window.ShowDialog() //blocking call
}
Window
you could have your add_user
control dynamically added to the visual tree of your existing MainWindow
, like this:<Grid>
<local:user_list x:Name="userList"></local:user_list>
<ContentControl x:Name="contentControl"></ContentControl>
</Grid>
And the code-behind:
private void Button_Click(object sender, EventArgs e) {
contentControl.Content = new add_user();
}
Upvotes: 1