ContentControl中没有更新 [英] ContentControl not updating
问题描述
我想有一个绑定到一个视图中的主窗口。我更改代码视图,并期待它在主窗口更新,但是这没有发生。
我有这样的代码在我的XAML
<预类=郎咸平的XML prettyprint-覆盖>
<网格和GT;
< ContentControl中CONTENT ={绑定源= {StaticResource的ViewModelLocator},路径= MainWindowViewModel.CurrentControl}/>
< /网格和GT;
然后我通过这段代码
改变我的控制<前类=郎-CS prettyprint-覆盖>
公共类MainWindowViewModel:ReactiveObject
{
私人用户控件_CurrentControl = NULL;
公众用户控件CurrentControl
{
得到
{
如果(_CurrentControl == NULL)
{
_CurrentControl =新的主页();
}
返回_CurrentControl;
}
组
{
this.RaiseAndSetIfChanged(X => x.CurrentControl,价值);
}
}
}
正如你可以看到我'使用ReactiveUI库米
是 ContentControl中
做错事在该视图中使用还是我只是不具约束力和更新是否正确?
有实际上是一个更好的办法做到这一点,使用 ViewModelViewHost
:
<电网的DataContext ={绑定视图模型,的ElementName = TheUserControl}>
< ViewModelViewHost视图模型={结合CurrentControlViewModel}/>
< /网格和GT;
现在,您的类看起来是这样的:
公共类MainWindowViewModel:ReactiveObject
{
私人ReactiveObject _CurrentControlViewModel =新HomePageViewModel();
公共ReactiveObject CurrentControlViewModel {
{返回_CurrentControl; }
集合{this.RaiseAndSetIfChanged(X => x.CurrentControlViewModel,价值); }
}
}
和地方在你的应用程序的启动,你应该写
RxApp.Register(typeof运算(IViewFor< HomePageViewModel>)的typeof(主页));
什么是ViewModelViewHost?
ViewModelViewHost
将带您通过绑定提供一个视图模型对象,而查找的适合它,使用服务定位视图。该注册电话是怎么可以关联与次的ViewModels
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?
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.
这篇关于ContentControl中没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!