ContentControl not updating

好久不见. 提交于 2019-12-04 12:55:30

问题


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 have this code in my XAML

<Grid>
    <ContentControl Content="{Binding Source={StaticResource ViewModelLocator}, Path=MainWindowViewModel.CurrentControl}" />
</Grid>

I then change my Control via this code

public class MainWindowViewModel : ReactiveObject
{
    private UserControl _CurrentControl = null;
    public UserControl CurrentControl
    {
        get
        {
            if (_CurrentControl == null)
            {
                _CurrentControl = new HomePage();
            }
            return _CurrentControl;
        }
        set
        {
            this.RaiseAndSetIfChanged(x => x.CurrentControl, value);
        }
    }
}

As you can see I'm using the ReactiveUI library.

Is ContentControl the wrong thing to use in that view or am I just not binding and updating correctly?


回答1:


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.




回答2:


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>



回答3:


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.



来源:https://stackoverflow.com/questions/15353500/contentcontrol-not-updating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!