[英]Is there a way to automatically expose properties and events of a standard wpf control wrapped in a user control?
我包装了这里讨论的标准控件。在WPF中继承标准控件的最快方法?
在用户控件中。 现在有没有一种方法可以自动公开包装在用户控件中的标准wpf控件的属性和事件? 我可以使用诸如自动冒泡路由之类的方法,但究竟如何呢?
否则,我还必须为我的组件创建麻烦的属性和事件包装器。
是的,您必须在用户控件上提供新的依赖项属性,以将属性公开给外部。 举个例子:
TimeframeSelector.xaml
<DatePicker SelectedDate="{Binding Path=StartDate,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type ctrl:TimeframeSelector}}}"/>
TimeframeSelector.xaml.cs
public DateTime StartDate
{
get{ return (DateTime)GetValue(StartDateProperty);}
set { SetValue(StartDateProperty, value); }
}
public static readonly DependencyProperty StartDateProperty =
DependencyProperty.RegisterAttached(
"StartDate",
typeof(DateTime),
typeof(TimeframeSelector),
new FrameworkPropertyMetadata(
DateTime.MinValue,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
您需要使用RelativeSource
绑定,否则将无法在视觉树中找到DP。 如果使用的是Visual Studio,则有一个名为propdp
模板,可用于快速创建依赖属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.