I have two UserControls
, for example, FirstUserControl
and SecondUserControl
. In first I set the FirstUserControl
in MainWindow with help <ContentControl x:Name="contentControl" />
.
public MainWindow()
{
InitializeComponent();
this.contentControl.Content = new LoginControl();
}
But, when user press the button, what located in the FirstUserControl ContentControl
in MainWindow must change to SecondUserControl. But it doesn't happen. ContentControl remains FirstUserControl. What could be the problem - I don't know.
This code is from FirstUserControl:
private void loginButton_Click(object sender, RoutedEventArgs e)
{
MainWindow mainWindow = new MainWindow(new CheckInCheckOutControl());
}
And it's consructor from MainWindow, whish is responsible for shanging ContentControl:
public MainWindow(ContentControl contentControl)
{
this.contentControl.Content = contentControl;
}
And I have tried also to carry out changes to a separate method in the MainWindow - but it didn't help.
MainWindow mainWindow = new MainWindow(new CheckInCheckOutControl());
creates a new instance of MainWindow, instead of using already open MainWindow. don't do that.
MainWindow owns FirstUserControl and SecondUserControl, therefore it is MainWindow who should change them, not FirstUserControl.
create an event in FirstUserControl
public event EventHandler LoginClick;
private void loginButton_Click(object sender, RoutedEventArgs e)
{
if (LoginClick != null) LoginClick(this, e);
}
in MainWindow, which owns FirstUserControl instance, subscribe to that event and change content:
public MainWindow()
{
InitializeComponent();
var loginControl = new LoginControl();
loginControl.LoginClick += (senser, e) =>
{
this.contentControl.Content = new CheckInCheckOutControl();
}
this.contentControl.Content = loginControl;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments