我有一个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