Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a bindable property in WPF?

Tags:

c#

binding

wpf

i have a user control. I want to create a bindable property in my user control. I create a DependencyProperty as follows:

public static readonly DependencyProperty DateProperty =
    DependencyProperty.Register("Date", typeof(DateTime), typeof(DaiesContainer), 
        new UIPropertyMetadata(DateTime.Now));

    public DateTime Date
    {
        get { return (DateTime)GetValue(DateProperty); }
        set
        {
            SetValue(DateProperty, value);
        }
    }

I then use it in my XAML:

<ctrls:DaiesContainer  Date="{Binding Date, Mode=OneWay}"/>

In my ViewModel, the get method of the Date property is called. But in my user control, Date property is not set to a value.

like image 453
Ali Jalali Avatar asked Jul 13 '13 11:07

Ali Jalali


People also ask

How to add dependency property WPF?

To create new dependency property we need to follow the below procedure, Declare and register dependency property. For registered property set value using SetValue method and get value using GetValue method. Write a method to handle change done on dependency property.

What is a bindable property?

Property bindings are one of the core features of QML. They allow to specify relationships between different object properties and automatically update properties' values whenever their dependencies change. Bindable properties allow to achieve the same not only in QML code, but also in C++.

What is dependency property in WPF with example?

In WPF applications, dependency property is a specific type of property which extends the CLR property. It takes the advantage of specific functionalities available in the WPF property system. A class which defines a dependency property must be inherited from the DependencyObject class.

What are attached properties in WPF?

An attached property is a Extensible Application Markup Language (XAML) concept. Attached properties enable extra property/value pairs to be set on any XAML element that derives from DependencyObject, even though the element doesn't define those extra properties in its object model.


1 Answers

Your dependency property implementation is missing a PropertyChangedCallback that gets called when the value of the property has changed. The callback is registered as a static method, which gets the current instance (on which the property has been changed) passed as its first parameter (of type DependencyObject). You have to cast that to your class type in order to access instance fields or methods, like show below.

public static readonly DependencyProperty DateProperty =
    DependencyProperty.Register("Date", typeof(DateTime), typeof(DaiesContainer),
    new PropertyMetadata(DateTime.Now, DatePropertyChanged));

public DateTime Date
{
    get { return (DateTime)GetValue(DateProperty); }
    set { SetValue(DateProperty, value); }
}

private void DatePropertyChanged(DateTime date)
{
    //...
}

private static void DatePropertyChanged(
    DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ((DaiesContainer)d).DatePropertyChanged((DateTime)e.NewValue);
}

Please note also that setting a default value of the dependency property is only done once for all instances of your class. Setting a value of DateTime.Now will hence produce the same default value for all of them, namely the time at which the static DependencyProperty is registered. I guess using something more meaningful, perhaps DateTime.MinValue, would be a better choice. As MinValue is already the default value of a newly created DateTime instance, you may even omit the default value from your property metadata:

public static readonly DependencyProperty DateProperty =
    DependencyProperty.Register("Date", typeof(DateTime), typeof(DaiesContainer),
    new PropertyMetadata(DatePropertyChanged));
like image 135
Clemens Avatar answered Sep 28 '22 11:09

Clemens