Load multiple UserControls on WPF window

后端 未结 2 1153
梦毁少年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条回答
  •  猫巷女王i
    2021-01-26 19:19

    Put a ContentPresenter in the MainWindow like this:

    
    

    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.

提交回复
热议问题