试图了解INotifyPropertyChanged [英] Trying to understand INotifyPropertyChanged
问题描述
几个(新手)问题:
1)我看到很多
public Person SelectedPerson { get; set; }
我假设这不会改变属性?
I am assuming this does NOT fire a property change?
所以,如果我想这样做,我必须提供以下内容?
So, if I want to do so, I must provide the following?
private Person selectedPerson;
public Person SelectedPerson
{
get
{
return this.selectedPerson;
}
set
{
if ((this.selectedPerson != value))
{
this.selectedPerson = value;
base.OnPropertyChanged("SelectedPerson");
// Note: Using ViewModelBase
}
}
}
2)如果我有以下条件:
2) If I have the following:
public bool CanDeletePerson
{
get
{
return SelectedPerson != null;
}
}
以及XAML
< Button IsEnabled="{Binding CanDeletePerson}"
Command="{Binding DeletePersonCommand}"> Delete </ Button >
如何让它重新评估CanDeletePerson属性?
基本上,它最初被禁用,但是当我点击网格并将行移动到行时,SelectedPerson IS被更新,但CanDeletePerson没有获得更新的值,那么如何让它实际检查值再次?
How do I get it to re-evaluate the CanDeletePerson property? Basically, it is initially disabled, but when I click on the grid and move row to row, the SelectedPerson IS being updated, but the CanDeletePerson is NOT getting the updated value, so how do I get it to actually check the value again?
3)我看到
...取决于是否有已订阅事件
... depends on whether anything has subscribed to the event
您如何订阅该活动?
推荐答案
- 正确 - 不触发
-
code> CanDeletePerson 重新评估,您可以在
CanDeletePerson
中调用PropertyChanged,如此
- Correct - that does not fire
Yes, to have
CanDeletePerson
reevaluated, you can raise PropertyChanged onCanDeletePerson
, like so
set
{
if ((this.selectedPerson != value))
{
this.selectedPerson = value;
base.OnPropertyChanged("SelectedPerson");
base.OnPropertyChanged("CanDeletePerson");
}
}
像任何正常的事件一样,但如果你使用WPF,您通常不需要明确订阅
Like any normal event, but if you're using WPF, you typically don't need to subscribe explicitly
这篇关于试图了解INotifyPropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!