Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom XAML attributes to a class that inherits UserControl?

Tags:

c#

wpf

xaml

I have a custom UserControl and I want to give it a custom property "MyProperty" which I can set in XAML. So that my XAML will look like this:

<EventDet:EventAddressControl 
            MyCustomProperty="formattype"     
            x:Name="EventSessionLocationControl"/>

How do I give the UserControl a custom attribute / property which I can then set in XAML?

like image 400
patrick Avatar asked Dec 21 '22 14:12

patrick


2 Answers

If you are using CLRProperty you cannot use for Binding purpose.

 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 ));
}
 <my:MyCustomControl MyProperty="{Binding BindingProperty}"
                     MyCLRProperty="MyCLRProperty"/>
like image 93
Kishore Kumar Avatar answered Jan 05 '23 00:01

Kishore Kumar


Just put a normal DependencyProperty in your class.

like image 40
SLaks Avatar answered Jan 04 '23 23:01

SLaks