我有一個WPF中的可視化控件,它利用了依賴屬性。這些屬性由作爲類的字段支持,並且有時需要通知所有綁定,實際上包含的類被修改時,屬性的值會發生更改。PropertyChanged未連接
簡單地說:
- MyDepProp是類型的MyClass;
- 由於控件的內部操作,MyClass的內容發生變化;
- 我想告訴大家,MyDepProp已更改,以便它們可以反映MyClass中的更改。
MSDN說,當第一次使用依賴項屬性時,PropertyChanged連接到DependencyObject。它在Visual Studio 2010中工作。但是,在安裝Visual Studio 2012之後,它停止工作:即使使用了DP(例如,綁定已附加到它),PropertyChanged爲null,我無法通知任何更改。
我仍然可以使用Visual Studio 2010的編譯器工具包,如此看來,它的破框架,這與2012年VS一起更新
我是否正確使用PropertyChanged事件的問題?還是VS 2012更新的.NET 4.0框架中的錯誤?有沒有人遇到類似的問題?
編輯:將一塊缺陷代碼:
public partial class MyImageControl : INotifyPropertyChanged,
IHandle<ImageRefresh>
{
// ***************************
// *** Dependency property ***
// ***************************
private void OnDataSourceChanged()
{
// ...
}
private static void DataSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is MyImageControl)
((MyImageControl)d).OnDataSourceChanged();
}
public static readonly DependencyProperty DataSourceProperty = DependencyProperty.Register("DataSource",
typeof(IDataSource),
typeof(MyImageControl),
new PropertyMetadata(null, DataSourceChanged));
public IDataSource DataSource
{
get
{
return (IDataSource)GetValue(DataSourceProperty);
}
set
{
SetCurrentValue(DataSourceProperty, value);
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("DataSource"));
}
}
// ***********************************
// *** INotifyPropertyChanged impl ***
// ***********************************
public event PropertyChangedEventHandler PropertyChanged;
// *************************************
// *** Method, which exposes the bug ***
// *************************************
public void Handle(ImageRefresh message)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("BackgroundKind"));
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("DataSource"));
}
}
作爲參考,IHandle接口:
public interface IBaseHandle { }
public interface IHandle<TMessage> : IBaseHandle
{
void Handle(TMessage message);
}
的情形:
- 的DataSource 是結合使用
Binding
- 有人另一個屬性調用控制(使用
IHandle
接口) - 手柄檢查的PropertyChanged是否不爲空的
Handle
方法,它是,所以沒有關於在數據源的改變信息被傳播。
你能表現出一定的代碼,這一切都在VS2012在我的電腦工作正常... – Peter 2013-03-28 10:25:55
這可能是困難的,它實際上是一個巨大的一部分項目,在許多地方這個機制的作品。這就是我無法輕鬆提供概念代碼的原因。如果我設法這樣做,我會在這裏發佈。這就是爲什麼我問,如果有人也遇到這個問題:) – Spook 2013-03-28 10:32:29
我現在在VS2012使用DP沒有任何問題。確保你有一個datacontext的實例,否則你可能會更新錯誤的。還要確保使用與屬性相同的名稱調用RaisePropertyChanged事件。如果沒有代碼,這些將是我要檢查的第一件事情。 – keyboardP 2013-03-28 10:53:44