【问题标题】:How to create a bindable property in WPF?如何在 WPF 中创建可绑定属性?
【发布时间】:2013-07-13 11:54:47
【问题描述】:

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

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 中使用它:

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

在我的 ViewModel 中,调用了 Date 属性的 get 方法。但在我的用户控件中,日期属性未设置为值。

【问题讨论】:

  • 您是否在 XAML 中将 DateTime 插入到 compatible pattern 中?
  • 怎么知道属性没有设置?您尚未使用依赖项属性的元数据注册 PropertyChangedCallback。请注意,在 XAML 中设置属性时不会调用 CLR 包装器的设置器,如 here 所述。
  • @Clemens 谢谢,这很好,但我必须调用非静态方法。

标签: c# wpf binding


【解决方案1】:

您的依赖属性实现缺少PropertyChangedCallback,当属性的值发生更改时会调用该PropertyChangedCallback。回调注册为静态方法,它获取当前实例(属性已更改)作为其第一个参数(DependencyObject 类型)传递。您必须将其转换为您的类类型才能访问实例字段或方法,如下所示。

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 实例的默认值,您甚至可以在属性元数据中省略默认值:

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

【讨论】:

  • 2020 年的建议:最好在 DependencyProperty 声明中使用 nameof(Date) 以防止更改属性名称后忘记更改此值
  • @AnatolyNikolaev 当然可以,但您不会在成千上万的旧答案中写此评论吗?
【解决方案2】:

让你的绑定模式双向

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

【讨论】:

  • 这行不通。当我在 C# 中为 date 属性设置一个值时,一切都很好,但是当我在 xaml 中为 date 属性设置一个值时,它没有设置。
【解决方案3】:

我认为您在 XAML UserControl 中有错误。

您应该通过ElementName绑定,例如:

<UserControl x:Class="WPFProject.DaiesContainer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             x:Name="daiesContainer">
    <Grid>
        <DatePicker SelectedDate="{Binding Date, ElementName=daiesContainer}" />
    </Grid>
</UserControl>

在这种情况下,您绑定到 DaiesContainer 类的 Date 属性。

如果你使用不带ElementName的绑定:

<DatePicker SelectedDate="{Binding Date}" />

UserControl DaiesContainer 将尝试在该用户控件的容器DataContext 中查找属性Date,如果找不到此属性,您将看到DatePicker 的选定值为空。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    相关资源
    最近更新 更多