0
用户控件之间的结合我有一个项目的解决方案如下WPF的MVVM:从不同的项目
solution with different project in MVVM
起始窗被MainWindowStart.xaml位于ProjectStart.View与DataContext的ProjectStartViewModel.cs。
MainWindowStart.xaml包括2个用户控件(MyUC1.xaml,MyUC2.xaml),它们位于一个不同的项目(PROJECT1)用的DataContext Project1ViewModel.cs as showen。这些属性正确绑定在MainWindow的InitializeComponent()上,但在属性更改时不起作用。 它工作正常时,用户控件包含在位于用户控制的一些项目窗口
这是财产的一个视图模型视图模型
private int _IndexTabMain;
public int IndexTabMain
{
get { return _IndexTabMain; }
set
{
if (_IndexTabMain != value)
{
_IndexTabMain = value;
this.RaisePropertyChanged("IndexTabMain");
}
}
}
的代码,这是代码财产MyUC2.xaml是从选择变化通知MyUC1.xaml
<TextBlock Text="{Binding DataContext.IndexTabMain, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=UserControl}}">
在预先绑定谢谢 曼努埃尔
作为一般规则,你不应该明确地设置用户控件的DataContext的。这样做有效地防止了DataContext从父控件或窗口继承,这通常会破坏您的Bindings。见例如这里:http://stackoverflow.com/a/28982771/1136211 – Clemens
定义的IndexTabMain属性在哪里? – mm8
您的UserControl应该在您的ViewModel绑定的表面上公开DependencyProperties。就像TextBox具有Text属性一样,您的用户控件应该有一个绑定到视图模型的Whatever属性的Whatever属性。 – Will