如何将自定义XAML属性添加到继承UserControl的类?

ast*_*ght 4 c# wpf xaml

我有一个自定义UserControl,我想给它一个自定义属性"MyProperty",我可以在XAML中设置.这样我的XAML将如下所示:

<EventDet:EventAddressControl 
            MyCustomProperty="formattype"     
            x:Name="EventSessionLocationControl"/>
Run Code Online (Sandbox Code Playgroud)

如何为UserControl提供一个自定义属性/属性,然后我可以在XAML中设置它?

Kis*_*mar 8

如果您使用的是CLRProperty,则不能用于绑定目的.

 public partial class MyCustomControl : UserControl
{
    public MyCustomControl()
    {
        InitializeComponent();
    }

    public string MyCLRProperty { get; set; }

    public string MyProperty
    {
        get { return (string)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty", typeof(string), typeof(MyCustomControl ));
}
Run Code Online (Sandbox Code Playgroud)
 <my:MyCustomControl MyProperty="{Binding BindingProperty}"
                     MyCLRProperty="MyCLRProperty"/>
Run Code Online (Sandbox Code Playgroud)