WPF : dynamic view/content

前端 未结 3 1097
无人共我
无人共我 2020-11-27 23:23

I\'m a bit beginner in WPF, so I ask this..

Let\'s say I have a window, and inside the window I want to have something like container, could be just border or maybe

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 23:34

    I solved this with a ContentControl

    MainWindow: (Define the views you wish to visualize as resources)

    
        
            
        
        
            
        
        
    
    
    
    
       
    
    

    ViewModel: (can't get much simpler)

    public ViewModelBase CurrentView
    {
        get { return currentView; }
        set { Set(() => CurrentView, ref currentView, value); }
    }
    

    And there you go, you can change your views by setting the view model for the controls you defined in your MainWindow

    private void OnCommandExecuted()
    {
        CurrentView = someViewModel;
    }
    
    private void OnAnotherCommandExecuted()
    {
        CurrentView = anotherViewModel;
    }
    

    HTH!

提交回复
热议问题