wpf 依赖属性的 PropertyChangedCallback 没有被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12745060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dependency Properties' PropertyChangedCallback not getting called
提问by schoola
If have an own user control with a DependencyProperty and the corresponding callback method like below:
如果有一个自己的带有 DependencyProperty 的用户控件和相应的回调方法,如下所示:
public partial class PieChart : UserControl
{
public static readonly DependencyProperty RatesProperty = DependencyProperty.Register("Rates", typeof(ObservableCollection<double>), typeof(PieChart),
new PropertyMetadata(new ObservableCollection<double>() { 1.0, 1.0 }, new PropertyChangedCallback(OnRatesChanged)));
[...]
public static void OnRatesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((PieChart)d).drawChart();
}
When using this user control, I bind an ObservableCollection called "Rates" to the RatesProperty. Rates and the related methods look like this:
使用此用户控件时,我将名为“Rates”的 ObservableCollection 绑定到 RatesProperty。费率和相关方法如下所示:
private ObservableCollection<double> rates;
public ObservableCollection<double> Rates
{
get { return this.rates; }
set
{
if (this.rates != value)
{
this.rates = value;
OnPropertyChanged("Rates");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
When I change the ObservableCollection Rates (e.g. with this.Rates = new ObservableCollection<double>() { 1.0, 2.0 }) the OnRatesChanged() method of the user-control is called like expected. But when I execute the following, it is not called:
当我更改 ObservableCollection Rates(例如使用this.Rates = new ObservableCollection<double>() { 1.0, 2.0 })时,用户控件的 OnRatesChanged() 方法按预期调用。但是当我执行以下操作时,它不会被调用:
this.Rates[0] = (double)1;
this.Rates[1] = (double)2;
OnPropertyChanged("Rates");
I expected that when I raise the PropertyChanged event with the correct property name, the corresponding callback in the user control is always called. Is that wrong? I found this thread: Getting PropertyChangedCallback of a dependency property always - Silverlightwhich covers silverlight but I think then that the same is true in WPF.
我期望当我使用正确的属性名称引发 PropertyChanged 事件时,始终调用用户控件中的相应回调。错了吗?我找到了这个线程:始终获取依赖属性的 PropertyChangedCallback - Silverlight涵盖了 Silverlight 但我认为在 WPF 中也是如此。
So the Framework in the background checks if the bound property (in my example "Rates") changed and only if it changed, it calls the associated callback, correct? Thus changing the elements of my collection has no effect, I always have to change the complete collection?
因此,后台的框架会检查绑定属性(在我的示例中为“Rates”)是否发生更改,并且仅当发生更改时,它才会调用关联的回调,对吗?因此更改我的集合的元素没有效果,我总是要更改完整的集合?
Thank you!
谢谢!
回答by Clemens
Your conclusion is right, your OnRatesChangedcallback will only be called when the Ratesdependency property is set to a new collection (or null).
你的结论是对的,你的OnRatesChanged回调只会在Rates依赖属性设置为新集合(或 null)时调用。
In order to get notified about changes in the collection, you would also have to register a NotifyCollectionChangedEventHandler:
为了获得有关集合更改的通知,您还必须注册NotifyCollectionChangedEventHandler:
private static void OnRatesChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var pieChart = (PieChart)d;
var oldRates = e.OldValue as INotifyCollectionChanged;
var newRates = e.NewValue as INotifyCollectionChanged;
if (oldRates != null)
{
oldRates.CollectionChanged -= pieChart.OnRatesCollectionChanged;
}
if (newRates != null)
{
newRates.CollectionChanged += pieChart.OnRatesCollectionChanged;
}
pieChart.drawChart();
}
private void OnRatesCollectionChanged(
object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
...
}
drawChart();
}

