如何创建一个WPF可绑定属性? [英] How to create a bindable property in WPF?

查看:216
本文介绍了如何创建一个WPF可绑定属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件。我要创建我的用户控件绑定属性。我创建的DependencyProperty如下:

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);
        }
    }

然后我用它在我的XAML:

I then use it in my XAML:

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

在我的ViewModel,日期属性的get方法被调用。但在我的用户控件,日期属性未设置​​为一个值。

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.

推荐答案

您依赖属性的实现缺少的 PropertyChangedCallback 当属性的值发生变化时调用。回调注册为一个静态方法,它获取当前实例(该财产已被更改)作为其第一个参数传递(类型为的DependencyObject )。你必须强制转换到类的类型,以便访问实例字段或方法,像下面展示。

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);
}


另外请注意,设置依赖项属性的默认值类的所有实例只进行一次。设置的 DateTime.Now 将因此产生的所有这些相同的默认值,即时间值在该静态的DependencyProperty 注册。我猜使用更有意义的事情,也许是 DateTime.MinValue ,将是一个更好的选择。由于 MINVALUE 已经是一个新创建的DateTime 实例的默认值,你甚至可以从你的财产是忽略默认值元数据:


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));

这篇关于如何创建一个WPF可绑定属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆