DependencyProperties,其中值未在本地存储

时间:2022-09-01 11:27:51

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):

我对为依赖外部源的属性创建DependencyProperty感到困惑。例如,在我正在编写的超声应用程序中,我目前在Managed C ++包装器中有以下内容(为简单起见,在此转换为C#,实现了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.

我的所有代码都在WPF中使用,我正在考虑如何将INotifyPropertyChanged更改为DependencyProperty,以及如果我从这些更改中受益。有大约30个与此类似的变量,其中大多数变量数据绑定到屏幕上的滑块,文本块或其他控件。

Would the following be correct for implementing a DependencyProperty for this object?

以下是否适合为此对象实现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));

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?

我从来没有见过这样的例子.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");
}

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?

另外,在旁注中,我预计在大多数属性是数据绑定的情况下性能提升,或者更确切地说,在许多参数不是数据绑定的情况下性能损失?

1 个解决方案

#1


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.

使用依赖项属性时,Get和Set方法必须只是this.GetValue()和this.SetValue()的简单包装器,原因是WPF不使用getter或setter来访问该值,因此您可以'取决于一直运行的额外代码。

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.

如果你确实需要这些属性作为依赖属性,那么创建一个标准的依赖属性,它有效地缓存你的超.GetParam(prmGain)结果,并在PropertyChanged事件中调用Ultras.SetParam(prmGain,value),无论如何都可以可靠地调用它。财产改变了。


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.

虽然我上面写的仍然是正确的,重读你的问题让我觉得你可能误解了依赖属性是什么。如果这个C ++包装器对象实际上是你的模型,那么你想继续使用INotifyPropertyChanged。依赖属性旨在由内部的WPF控件使用,以便于绑定并降低控件的内存占用。它们不应该在数据提供者中使用,这是通知接口的用途。

#1


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.

使用依赖项属性时,Get和Set方法必须只是this.GetValue()和this.SetValue()的简单包装器,原因是WPF不使用getter或setter来访问该值,因此您可以'取决于一直运行的额外代码。

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.

如果你确实需要这些属性作为依赖属性,那么创建一个标准的依赖属性,它有效地缓存你的超.GetParam(prmGain)结果,并在PropertyChanged事件中调用Ultras.SetParam(prmGain,value),无论如何都可以可靠地调用它。财产改变了。


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.

虽然我上面写的仍然是正确的,重读你的问题让我觉得你可能误解了依赖属性是什么。如果这个C ++包装器对象实际上是你的模型,那么你想继续使用INotifyPropertyChanged。依赖属性旨在由内部的WPF控件使用,以便于绑定并降低控件的内存占用。它们不应该在数据提供者中使用,这是通知接口的用途。