试图了解INotifyPropertyChanged的 [英] Trying to understand INotifyPropertyChanged
问题描述
若干(新手)问题:
1)我看到了很多的
公众人物SelectedPerson {搞定;组; }
我假定这不火属性更改?
所以,如果我想这样做,我必须提供以下
私募人士selectedPerson?;
公众人物SelectedPerson
{
得到
{
返回this.selectedPerson;
}
组
{
如果((this.selectedPerson =值)!)
{
this.selectedPerson =价值;
base.OnPropertyChanged(SelectedPerson);
//注意:使用ViewModelBase
}
}
}
2)如果我有以下几点:
公共BOOL CanDeletePerson
{
得到
{
返回SelectedPerson!= NULL;
}
}
和也在XAML
<按钮IsEnabled ={结合CanDeletePerson}
命令={结合DeletePersonCommand}>删除< /按钮>
我如何得到它的重新评估CanDeletePerson财产?
基本上,它最初是禁用的,但是当我点击电网和移动行与行的SelectedPerson正在更新,但CanDeletePerson没有得到更新的价值,所以我怎么得到它实际上是检查值再?
3)我看
...取决于是否什么已订阅的事件。
块引用>
你如何订阅事件?
解决方案
- 正确 - 不火
是的,有
CanDeletePerson
重新评估,可以在CanDeletePerson
,像这样设置
{
如果((this.selectedPerson =值)!)
{
this.selectedPerson =价值;
base.OnPropertyChanged(SelectedPerson);
base.OnPropertyChanged(CanDeletePerson);
}
}
像任何正常事件,但如果你'再使用WPF,你通常不需要明确订阅
Several (newbie) questions:
1) I see a lot of
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) If I have the following:
public bool CanDeletePerson { get { return SelectedPerson != null; } }
and also in the XAML
< Button IsEnabled="{Binding CanDeletePerson}" Command="{Binding DeletePersonCommand}"> Delete </ Button >
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) I see
... depends on whether anything has subscribed to the event
How do you subscribe to the event?
解决方案
- Correct - that does not fire
Yes, to have
CanDeletePerson
reevaluated, you can raise PropertyChanged onCanDeletePerson
, like soset { if ((this.selectedPerson != value)) { this.selectedPerson = value; base.OnPropertyChanged("SelectedPerson"); base.OnPropertyChanged("CanDeletePerson"); } }
Like any normal event, but if you're using WPF, you typically don't need to subscribe explicitly
这篇关于试图了解INotifyPropertyChanged的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!