Changing content dynamically in WPF window from UserControl

Hanna Bilous

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.

ASh

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically changing UserControl content at run time with WPF/MVVM

From Dev

How to Pass a Value From a Window to a UserControl in WPF

From Dev

How to Pass a Value From a Window to a UserControl in WPF

From Dev

WPF - Passing parameter from Dialog Window to UserControl

From Dev

UserControl in a Window not working WPF

From Dev

Cast WPF Window to WPF UserControl

From Dev

Cast WPF Window to WPF UserControl

From Dev

WPF Binding a Main Window Control from a UserControl View Model

From Dev

Adding Children to StackPanel in Parent Window from UserControl in WPF/C#

From Dev

How to call a Usercontrol from another in same window in WPF ?

From Dev

Adding Children to StackPanel in Parent Window from UserControl in WPF/C#

From Dev

WPF Changing ContentTemplate in UserControl by Trigger

From Dev

WPF Changing ContentTemplate in UserControl by Trigger

From Dev

WPF View - UserControl path instead of UserControl content

From Dev

show a usercontrol in wpf window by button

From Dev

show a usercontrol in wpf window by button

From Dev

WPF window/usercontrol inheritance XAML

From Dev

Translating from Forms UserControl to WPF UserControl

From Dev

WPF MVVM changing parent window viewmodel from icommand execution

From Dev

WPF MVVM changing parent window viewmodel from icommand execution

From Dev

Passing Parameters between xaml window and usercontrol WPF

From Dev

WPF - Setting window size to child usercontrol size

From Dev

Controlling Window close from UserControl

From Dev

Access Window ViewModel from UserControl

From Dev

Controlling Window close from UserControl

From Dev

Dynamically changing ItemsSource of a WPF ComboBox

From Dev

changing contents of a grid in wpf dynamically

From Dev

WPF Data Binding From UserControl

From Dev

Access UserControl elements/properties from main window in a WPF XAML PowerShell script

Related Related

  1. 1

    Dynamically changing UserControl content at run time with WPF/MVVM

  2. 2

    How to Pass a Value From a Window to a UserControl in WPF

  3. 3

    How to Pass a Value From a Window to a UserControl in WPF

  4. 4

    WPF - Passing parameter from Dialog Window to UserControl

  5. 5

    UserControl in a Window not working WPF

  6. 6

    Cast WPF Window to WPF UserControl

  7. 7

    Cast WPF Window to WPF UserControl

  8. 8

    WPF Binding a Main Window Control from a UserControl View Model

  9. 9

    Adding Children to StackPanel in Parent Window from UserControl in WPF/C#

  10. 10

    How to call a Usercontrol from another in same window in WPF ?

  11. 11

    Adding Children to StackPanel in Parent Window from UserControl in WPF/C#

  12. 12

    WPF Changing ContentTemplate in UserControl by Trigger

  13. 13

    WPF Changing ContentTemplate in UserControl by Trigger

  14. 14

    WPF View - UserControl path instead of UserControl content

  15. 15

    show a usercontrol in wpf window by button

  16. 16

    show a usercontrol in wpf window by button

  17. 17

    WPF window/usercontrol inheritance XAML

  18. 18

    Translating from Forms UserControl to WPF UserControl

  19. 19

    WPF MVVM changing parent window viewmodel from icommand execution

  20. 20

    WPF MVVM changing parent window viewmodel from icommand execution

  21. 21

    Passing Parameters between xaml window and usercontrol WPF

  22. 22

    WPF - Setting window size to child usercontrol size

  23. 23

    Controlling Window close from UserControl

  24. 24

    Access Window ViewModel from UserControl

  25. 25

    Controlling Window close from UserControl

  26. 26

    Dynamically changing ItemsSource of a WPF ComboBox

  27. 27

    changing contents of a grid in wpf dynamically

  28. 28

    WPF Data Binding From UserControl

  29. 29

    Access UserControl elements/properties from main window in a WPF XAML PowerShell script

HotTag

Archive