1 回答
TA贡献1827条经验 获得超4个赞
更改对象的属性值不会更改对象的引用。
声明这个
public Test TestVM
{
get { return testVM; }
set
{
SetProperty(ref testVM, value);
StartTestCommand.RaiseCanExecuteChanged();
}
}
您基本上是在告诉编译器:TestVM更改对对象的引用(甚至更改为相同的值)时,请更新StartTestCommand的状态。
但是很明显,一旦分配了该对象,就不会更改对该对象的引用。
如果您希望ViewModel在某些子视图模型的(Test)属性更改时更新父视图模型()中的命令,则可以使用以下PropertyChanged事件:
public Test TestVM
{
get { return testVM; }
set
{
Test oldValue = testVM;
if (SetProperty(ref testVM, value))
{
if (oldValue != null)
{
oldValue.PropertyChanged -= TestPropertyChanged;
}
if (testVM!= null)
{
testVM.PropertyChanged += TestPropertyChanged;
}
}
}
}
void TestPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// filter if necessary
if (e.PropertyName == "...")
{
StartTestCommand.RaiseCanExecuteChanged();
}
}
- 1 回答
- 0 关注
- 421 浏览
添加回答
举报
