ContentControl not updating

后端 未结 3 2200
一个人的身影
一个人的身影 2021-02-10 07:28

I\'m trying to have a MainWindow that is bound to the a view. I change that view in code and expect it to update in the Main Window, however that is not happening.

I hav

3条回答
  •  日久生厌
    2021-02-10 07:52

    There is actually a far better way to do this, using ViewModelViewHost:

    
        
    
    

    Now, your class will look something like:

    public class MainWindowViewModel : ReactiveObject
    {
        private ReactiveObject _CurrentControlViewModel = new HomePageViewModel();
        public ReactiveObject CurrentControlViewModel {
            get { return _CurrentControl; }
            set { this.RaiseAndSetIfChanged(x => x.CurrentControlViewModel, value); }
        }
    }
    

    And somewhere in your app's startup, you should write:

    RxApp.Register(typeof(IViewFor), typeof(HomePage));
    

    What's ViewModelViewHost?

    ViewModelViewHost will take a ViewModel object that you provide via Bindings, and look up a View that fits it, using Service Location. The Register call is how you can associate Views with ViewModels.

提交回复
热议问题