Reputation: 1
I want to build an application based on a contentcontrol. My goal is to have a main window with a contentcontrol element and several usercontrol elements which are presented through this contentcontrol. My problem is: I need a way to show the usercontrol and perform an action in it. After the action is performed the usercontrol should like return a value (or similar) to the parent window with the contentcontrol element so that I am able to renew the content of the contentcontrol. I hope you can follow this terrible explanation.
Here an example: I have the Main Window with the contentcontrol element. While initializing this main window I set the Content property of the contentcontrol to a new UserControl element called "loginview" so that the loginview is presented within the main window. From there I perform the actions in the loginview like entering username and password. After everythin is done I click on the button "login" and check the entered values whether they are correct or not. When the check is successful I want to like tell the MainWindow "im done" and set a new content. So when the action was performed I want to be back in the code of the MainWindow.
Can anybody tell me how I can do this "return to parent" thing?
I tried to get the parent element with Window parentWindow = Window.GetWindow(this.Parent);
but that is not really what I wanted.
I want something like if the usercontrol were a new window which gets called by
LoginView window = new LoginView(); window.Show();
After the window is called I return to the Code from the Mainwindow.
Upvotes: 0
Views: 73
Reputation: 36629
WPF is biased towards the MVVM pattern. With this you split your UI into Views (i.e. xaml files) and ViewModels (i.e. .cs files) and use bindings to connect the Views to View models.
To use multiple views you have the main view include each sub-view you could do something like:
MainViewModel.cs:
public class MainViewModel : INotifyPropertyChanged
{
public Login{get;}
public MySubViewModel {get;}
public MainViewModel(){
Login = new LoginViewModel();
MySubViewModel = new SubViewModel();
}
// Implementation of INotifyPropertyChanged
}
MainView.xaml:
<StackPanel>
<LoginView DataContext={Binding Login}/>
<SubView DataContext={Binding MySubViewModel2 }/>
</StackPanel>
You can have a IsLoggedInProperty on your LoginViewModel
public class LoginViewModel : INotifyPropertyChanged
private bool isUserLoggedIn;
public LoginViewModel(){
}
public bool IsUserLoggedIn{
get => isUserLoggedIn;
set {
isUserLoggedIn = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsUserLoggedIn)));
}
}
// Implementation of INotifyPropertyChanged
This allow your MainView to bind to this property like
<Button IsEnabled={Binding Login.IsUserLoggedIn>
.
Note that the MVVM pattern makes it slightly cumbersome to handle buttons, since buttons want to bind to a ICommand
. A possible solution is to make a DelegateCommand that lets the button invoke a method, without the need to create a class for each button.
Upvotes: 0