【发布时间】:2009-06-19 14:43:18
【问题描述】:
我对为依赖于外部资源的属性创建 DependencyProperty 有点困惑。例如,在我正在编写的超声波应用程序中,我目前在托管 C++ 包装器中有以下内容(为了简单起见,此处翻译为 C#,实现 INotifyPropertyChanged):
public int Gain
{
get { return ultrasound.GetParam(prmGain); }
set
{
ultrasound.SetParam(prmGain, value);
NotifyPropertyChanged("Gain");
}
}
我所有的代码都在 WPF 中使用,我正在考虑如何将 INotifyPropertyChanged 更改为 DependencyProperty,以及我是否会从这些更改中受益。大约有 30 个与这个类似的变量,其中大部分都被数据绑定到屏幕上的滑块、文本块或其他控件。
以下对于为此对象实现DependencyProperty 是否正确?
public int Gain
{
get { return ultrasound.GetParam(prmGain); }
set
{
ultrasound.SetParam(prmGain, value);
this.SetValue(GainProperty, value);
}
}
public static readonly DependencyProperty GainProperty = DependencyProperty.Register(
"Gain", typeof(int), typeof(MyUltrasoundWrapper), new PropertyMetadata(0));
我从未见过不使用this.GetValue(GainProperty) 的示例。此外,还有其他功能可能会更改该值。这也是正确的改变吗?
public void LoadSettingsFile(string fileName)
{
// Load settings...
// Gain will have changed after new settings are loaded.
this.SetValue(GainProperty, this.Gain);
// Used to be NotifyPropertyChanged("Gain");
}
另外,附带说明一下,我应该期望在大多数属性是数据绑定的情况下获得性能提升,或者更确切地说,在许多参数不是数据绑定的情况下性能损失?
【问题讨论】:
标签: c# wpf dependency-properties managed-c++