Load multiple UserControls on WPF window

后端 未结 2 1140
梦毁少年i
梦毁少年i 2021-01-26 18:33

I have a Window on which I load an UserControl say Control1. Now if the user clicks on a certain button a new UserControl, Control2 should be loaded on the Window and Control1 s

相关标签:
2条回答
  • 2021-01-26 19:15

    Rachel's comment helped me to find the solution.

    0 讨论(0)
  • 2021-01-26 19:19

    Put a ContentPresenter in the MainWindow like this:

    <ContentPresenter Content="{Binding ActiveWidget}"/>
    

    and then in the ViewModel

    public ViewModelBase ActiveWidget {get;set;} // Don't forget INotifyPropertyChanged!!
    

    then you must create a DataTemplate for each ViewModel with an appropiate instance of the UserControls. See This Article for a generic solution to this.

    0 讨论(0)
提交回复
热议问题