何时在自定义控件中使用 属性 而不是 Dependency 属性?
When to use a Property over a Dependency Property in a custom control?
一段时间以来,我一直在想什么时候应该在自定义控件中使用 属性 或依赖关系 属性,所以我认为问一问是个好主意。
假设我有一个简单的(我的意思是,非常简单的)UserControl,里面只有一个 Button 和一个 TextBlock。让我们调用 TextBlock "myTextBlock".
现在,如果我知道我需要将该 TextBlock 的文本绑定到其他东西,我会使用依赖关系 属性,那就是没问题。
但是如果我只是 需要通过XAML 将TextBlock 的Text 属性 设置为某个值并保持不变怎么办?
如果我使用 属性 而不是 Dependency属性 有关系吗?
一个选项 faster/better 是否优于另一个选项,如果是,为什么?
这是一个包含两个选项的示例:
public String ButtonText
{
get { return (String)GetValue(ButtonTextProperty); }
set { SetValue(ButtonTextProperty, value); }
}
public static readonly DependencyProperty ButtonTextProperty =
DependencyProperty.Register("ButtonText", typeof(String), typeof(MyUserControl),
new PropertyMetadata(String.Empty, OnButtonTextPropertyChanged));
private static void OnButtonTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(MyUserControl)d.myTextBlock.Text = (String)e.NewValue;
}
并且只有 属性:
public String ButtonText
{
get { return myTextBlock.Text; }
set { myTextBlock.Text = value; }
}
我通常使用简单的 属性,但我不知道这是否是此处使用的更好方法。当然可以,没问题。
提前致谢!
塞尔吉奥
我总是问这些基本问题
Do I need this to be bindable, animations, or styles and have default
settings that a simple property can't offer for this control?
Do I need to preserve the XAML (expression) with this new property when I am going to serialize/deserialize this
control?
如果是,那我就用DependencyProperty
。如果没有,那么我将只使用在此控件中使用的简单属性。这类似于您是否要在对象中使用 fields
与 properties
的问题。我的建议是,当您在 属性 中需要框架的功能时使用它,因为它们会为您提供更多选择。您想知道是否需要这些选项。
您要使用 DependencyProperty 的另一个原因是因为您有一个 base/child 控制关系,并且您希望其他开发人员能够使用它。您无需创建接口来向您的控件公开,而是提供即时静态 getter/setter 至 read/modify 控件的值。当您创建 DependencyProperty
.
时,您已经在创建您的界面
如有疑问,请始终针对您的控件创建简单的 properties/fields。
我不同意总是伴随着 DependencyProperty
的评论,因为与简单的 属性. [=19= 相比,它并不便宜]
例如,您想更改 属性 而它是 DependencyProperty
那么很可能,我需要手动编辑 属性 这是一个 string
并且容易出错。现在将它与一个简单的 属性 进行比较,它给我一个编译时检查,我可以轻松找到引用并对其进行一些更改。
我曾有过 DependencyProperty
矫枉过正的经历,应该只使用简单的属性,其他开发人员可以清楚地阅读其意图。
一段时间以来,我一直在想什么时候应该在自定义控件中使用 属性 或依赖关系 属性,所以我认为问一问是个好主意。
假设我有一个简单的(我的意思是,非常简单的)UserControl,里面只有一个 Button 和一个 TextBlock。让我们调用 TextBlock "myTextBlock".
现在,如果我知道我需要将该 TextBlock 的文本绑定到其他东西,我会使用依赖关系 属性,那就是没问题。
但是如果我只是 需要通过XAML 将TextBlock 的Text 属性 设置为某个值并保持不变怎么办? 如果我使用 属性 而不是 Dependency属性 有关系吗? 一个选项 faster/better 是否优于另一个选项,如果是,为什么?
这是一个包含两个选项的示例:
public String ButtonText
{
get { return (String)GetValue(ButtonTextProperty); }
set { SetValue(ButtonTextProperty, value); }
}
public static readonly DependencyProperty ButtonTextProperty =
DependencyProperty.Register("ButtonText", typeof(String), typeof(MyUserControl),
new PropertyMetadata(String.Empty, OnButtonTextPropertyChanged));
private static void OnButtonTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(MyUserControl)d.myTextBlock.Text = (String)e.NewValue;
}
并且只有 属性:
public String ButtonText
{
get { return myTextBlock.Text; }
set { myTextBlock.Text = value; }
}
我通常使用简单的 属性,但我不知道这是否是此处使用的更好方法。当然可以,没问题。
提前致谢!
塞尔吉奥
我总是问这些基本问题
Do I need this to be bindable, animations, or styles and have default settings that a simple property can't offer for this control?
Do I need to preserve the XAML (expression) with this new property when I am going to serialize/deserialize this control?
如果是,那我就用DependencyProperty
。如果没有,那么我将只使用在此控件中使用的简单属性。这类似于您是否要在对象中使用 fields
与 properties
的问题。我的建议是,当您在 属性 中需要框架的功能时使用它,因为它们会为您提供更多选择。您想知道是否需要这些选项。
您要使用 DependencyProperty 的另一个原因是因为您有一个 base/child 控制关系,并且您希望其他开发人员能够使用它。您无需创建接口来向您的控件公开,而是提供即时静态 getter/setter 至 read/modify 控件的值。当您创建 DependencyProperty
.
如有疑问,请始终针对您的控件创建简单的 properties/fields。
我不同意总是伴随着 DependencyProperty
的评论,因为与简单的 属性. [=19= 相比,它并不便宜]
例如,您想更改 属性 而它是 DependencyProperty
那么很可能,我需要手动编辑 属性 这是一个 string
并且容易出错。现在将它与一个简单的 属性 进行比较,它给我一个编译时检查,我可以轻松找到引用并对其进行一些更改。
我曾有过 DependencyProperty
矫枉过正的经历,应该只使用简单的属性,其他开发人员可以清楚地阅读其意图。