值未存储在本地的 DependencyProperties
我对为依赖于外部源的属性创建 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");
}
另外,顺便说一句,在大多数属性都是数据绑定的情况下,我是否应该期望性能提升,或者更确切地说,在许多参数不是数据绑定的情况下,性能损失?
I'm a little confused on creating a DependencyProperty
for properties that depend on external sources. For example, in an ultrasound application I'm writing, I currently have the following in a Managed C++ wrapper (translated to C# for simplicity on here, implementing INotifyPropertyChanged):
public int Gain
{
get { return ultrasound.GetParam(prmGain); }
set
{
ultrasound.SetParam(prmGain, value);
NotifyPropertyChanged("Gain");
}
}
All my code is used in WPF, and I was considering how changing the INotifyPropertyChanged
to DependencyProperty
would happen, and if I'd benefit from the changes. There are around 30 variables similar to this one, most of which get databound to an on-screen slider, textblock, or other control.
Would the following be correct for implementing a DependencyProperty
for this object?
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));
I've never seen an example where this.GetValue(GainProperty)
wasn't used. Also, there are other functions that may change the value. Would this be the correct change as well?
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");
}
Also, on a side note, should I expect performance gains in cases where most of the properties are databound, or rather, performance loss in cases where many parameters are NOT databound?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用依赖项属性时,Get 和 Set 方法必须只是 this.GetValue() 和 this.SetValue() 的简单包装器,原因是 WPF 不使用 getter 或 setter 来访问值,因此您可以'不依赖于一直运行的额外代码。
如果您确实需要将这些属性作为依赖属性,则创建一个标准依赖属性,该属性可以有效地缓存您的 ultrason.GetParam(prmGain) 结果,并在 PropertyChanged 事件中调用 ultrason.SetParam(prmGain, value),无论事件如何,该事件都会被可靠地调用。属性被改变。
虽然我上面写的仍然是正确的,但重读您的问题让我认为您可能误解了依赖属性是什么。 如果这个 C++ 包装对象实际上是您的模型,那么您希望继续使用 INotifyPropertyChanged。 依赖属性设计为供 WPF 控件内部使用,以促进绑定并降低控件的内存占用量。 它们不应该在数据提供者内部使用,这就是通知接口的用途。
When using a dependency property the Get and Set methods must just be simple wrappers for this.GetValue() and this.SetValue(), the reason for this is that WPF does not use the getters or setters to access the value so you can't depend on the extra code running all the time.
If you do need these properties to be dependency properties then create a standard dependency property which effectively caches your ultrasound.GetParam(prmGain) result and call ultrasound.SetParam(prmGain, value) inside the PropertyChanged event which will reliably be called no matter how the property is changed.
While what I have written above is still correct, rereading you question makes me think that you may be misunderstanding what a dependency property is. If this C++ wrapper object is effectively your model then you want to stay with INotifyPropertyChanged. Dependency properties are designed to be used by a WPF control internally to facilitate binding and lower the control's memory footprint. They aren't supposed to be used inside the data provider, that is what the notification interfaces are for.