1 回答

TA貢獻1827條經(jīng)驗 獲得超4個贊
更改對象的屬性值不會更改對象的引用。
聲明這個
public Test TestVM
{
get { return testVM; }
set
{
SetProperty(ref testVM, value);
StartTestCommand.RaiseCanExecuteChanged();
}
}
您基本上是在告訴編譯器:TestVM更改對對象的引用(甚至更改為相同的值)時,請更新StartTestCommand的狀態(tài)。
但是很明顯,一旦分配了該對象,就不會更改對該對象的引用。
如果您希望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 關(guān)注
- 355 瀏覽
添加回答
舉報