ContentControl not updating

后端 未结 3 2199
一个人的身影
一个人的身影 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:48

    I think you have several muddled concepts here and they are getting in each others way.

    Firstly you aren't actually using ANY of the reactiveUI code, it never gets called. Since your get accessor implements a lazy instantiation pattern then it means the set accessor is ignored. This means that the view never notifies the property change, so you never get updates.

    I'd recommend using something more like

    private UserControl _currentControl;
    
    public MainWindowVirwModel()
    {
      CurrentControl = new HomePage();
    }
    
    public UserControl CurrentControl
    {
      get { return _curentControl;}
      set { this.RaiseAndSetIfChanged(...); }
    }
    

    In addition, this still mixes up View components i.e. HomePage, inside your ViewModel tier which will making unit testing far more difficult.

    0 讨论(0)
  • 2021-02-10 07:52

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

    <Grid DataContext="{Binding ViewModel, ElementName=TheUserControl}">
        <ViewModelViewHost ViewModel="{Binding CurrentControlViewModel}" />
    </Grid>
    

    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<HomePageViewModel>), 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.

    0 讨论(0)
  • 2021-02-10 07:53

    why you call your class MainWindowViewModel? when you wanna do mvvm you shouldn't have properties with type UserControl in your VM.

    the usual mvvm way looks like this:

    • viewmodel with INotifyPropertyChanged
    public class MyViewmodel
    {
        public IWorkspace MyContent {get;set;}
    }
    
    • xaml content control with binding to your VM
    <ContentControl Content="{Binding MyContent}"/>
    
    • datatemplate --> so that wpf knows how to render your IWorkspace
    <DataTemplate DataType="{x:Type local:MyIWorkSpaceImplementationType}" >
       <view:MyWorkspaceView />
    </DataTemplate>
    
    0 讨论(0)
提交回复
热议问题