何时使用依赖项属性 [英] When to use Dependency Properties
问题描述
我有时认为我可能不必要地使用了依赖项属性。我什么时候需要使用它?当我拥有一个依赖于其他属性的属性时?假设我有一个 Color
属性,我希望它依赖于属性色相,饱和度,亮度我是否使用依赖项属性?还是我用什么?我控制绑定到颜色
的多数民众赞成在属性色相,饱和度,亮度更改时进行更新。
I sometimes think I maybe using Dependency Properties unnecessarily. When do I need to use it? When I have a property that dependes on other properties? Say I have a Color
property that I want it to be dependent on properties Hue, Saturation, Luminosity do I use a dependency property? Or what do I use? I controls thats bound to Color
to update when properties Hue, Saturation, Luminosity are changed.
目前我所做的是
public byte Hue {
get { return _hue; }
set
{
if (_hue == value)
return;
_hue = value;
NotifyPropertyChanged("Hue");
NotifyPropertyChanged("Color"); // to update controls bound to color
}
}
但是我认为这不是正确的处事方式吗?如果我有更多影响颜色的属性,那么所有这些属性中都会有1条额外的行吗?
But I think this is not the right way of doing things? If I have more properties that affect color, I will have 1 extra line in all those properties?
推荐答案
您应该只使用一个 DependencyProperty
当您希望能够通过XAML将其值绑定到某项内容时,例如
You should only use a DependencyProperty
when you want to be able to bind its value to something through XAML, e.g.
<local:MyObject MyDependencyProperty="{Binding ...}" />
更新:如以下Ian所述,如果要进行动画处理,还需要依赖项属性您的财产或通过样式进行设置
Update: as mentioned by Ian below, dependency properties are also required if you want to be able to animate your property or set it through a style
如果您不需要以这种方式工作,那就没有必要了。例如如果您只想通过XAML将值设置为常量(如下所示),则无需使用 DependencyProperty
If you do not need to work in this way then it is unnecessary. e.g. If you just want to be able to set the value to a constant through XAML (as below) this will work without using a DependencyProperty
<local:MyObject MyRegularProperty="Some Value" />
类似地,如果您想将绑定到 (例如)您的视图模型:
Similarly, if you want to bind to the value of a property on (for example) your view model:
<TextBlock Text="{Binding MyViewModelProperty}" />
然后,您不需要使用 DependencyProperty
。前提是您实现了 INotifyPropertyChanged
,则当属性更改时,文本
仍将更新。
then you do not need to use a DependencyProperty
. Provided that you implement INotifyPropertyChanged
then the Text
will still be updated when the property changes.
编辑:在重新阅读您的问题时,我不确定您是否使用 DependencyProperty来影响您的处境
-如果我正确地阅读了它,那么您要做的就是在这些属性中的任何一个发生更改时,在UI上更新许多属性,对吗?
on re-reading your question, I am not sure whether or not your situation will be affected by whether or not you use a DependencyProperty
- if I'm reading it correctly, all you want to do is cause a number of properties to be updated on the UI when any one of those properties changes, right?
我认为您目前的执行方式没有任何问题(即在每个设置器中引发很多 PropertyChanged
事件),但是如果您不热衷于此,则可以尝试使用一个单个属性来公开相关的子属性以进行绑定:
I don't think there is anything wrong with how you are implementing things at the moment (i.e. raising a lot of PropertyChanged
events in each setter), but if you aren't keen on in then you could try having a single property that exposes relevant child properties to bind to that are all calculated:
class ColorWrapper
{
public Color Color { get; set; }
public byte Hue
{
get { return this.Color.Hue; } //or however this is calculated
}
然后有一个 Color
属性引发 PropertyChanged
事件并通过View绑定到该事件:
Then have a Color
property on your ViewModel that raises the PropertyChanged
event and bind to that through the View:
<TextBlock Text="{Binding Color.Hue}" />
正如我所说,我不会说这是对您已经拥有的东西的特别改进
As I said, I wouldn't say that this is particularly an improvement on what you have already though.
这篇关于何时使用依赖项属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!