【发布时间】:2012-01-20 10:59:57
【问题描述】:
假设我有一个自定义控件,它包装了另一个控件(例如 MyCustomButton)。我暴露了一个属性Content,它封装了内部控件:
public object Content
{
get { return innerControl.Content; }
set { innerControl.Content = value; }
}
为了让消费者绑定到这个属性,我需要为它定义一个 DependencyProperty:
public static DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof (object), typeof (MyCustomButton));
但现在我需要我的属性定义才能使用 GetValue/SetValue:
public object Content
{
get { return GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
所以我不再包装内部控件的值了。
我可以定义 PropertyMetadata 来处理 DependencyProperty 的 PropertyChanged 事件,但是我需要一堆管道代码来保持值同步并防止更改时无限循环。
更新:我不能只从 Button 派生,因为我的 UserControl 有各种其他问题。
有没有更好的方法来做到这一点?
【问题讨论】:
标签: wpf xaml dependency-properties