简体   繁体   English

有没有一种方法可以自动公开包装在用户控件中的标准wpf控件的属性和事件?

[英]Is there a way to automatically expose properties and events of a standard wpf control wrapped in a user control?

I wrapped a standard control as discussed here Quickest way to inherit a standard control in WPF? 我包装了这里讨论的标准控件。在WPF中继承标准控件的最快方法?

in a user control. 在用户控件中。 Now is there a way to automatically expose properties and events of a standard wpf control wrapped in a user control ? 现在有没有一种方法可以自动公开包装在用户控件中的标准wpf控件的属性和事件? Could I use something like automatic Bubbling routing but how exactly ? 我可以使用诸如自动冒泡路由之类的方法,但究竟如何呢?

Otherwise I'll have to also create properties and events wrappers for my component that is cumbersome. 否则,我还必须为我的组件创建麻烦的属性和事件包装器。

Yes, you have to provide new Dependency Properties on the user control to expose properties to the outside. 是的,您必须在用户控件上提供新的依赖项属性,以将属性公开给外部。 As an example: 举个例子:

TimeframeSelector.xaml TimeframeSelector.xaml

<DatePicker SelectedDate="{Binding Path=StartDate, 
                            RelativeSource={RelativeSource Mode=FindAncestor, 
                              AncestorType={x:Type ctrl:TimeframeSelector}}}"/>

TimeframeSelector.xaml.cs 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));

You need to use the RelativeSource binding as you won't find the DP in the visual tree otherwise. 您需要使用RelativeSource绑定,否则将无法在视觉树中找到DP。 If you are using Visual Studio, there is template named propdp which you can use to create depedendency properties very fast. 如果使用的是Visual Studio,则有一个名为propdp模板,可用于快速创建依赖属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM