WPF MVVM 依赖属性

标签 wpf mvvm wpf-controls dependency-properties

我有以下情况:

  • 我有一个用户控件,里面只有一个网格。
  • Grid 的第一列是复选框列,它绑定(bind)到 CustomerModel
  • 的 IsSelected 属性
  • 网格的 ItemsSource 绑定(bind)到 List
  • 当用户选中任何 CheckBox 时,CustomerModel 的相应 IsSelected 属性正在更新

  • 询问:
  • 我向名为“SelectedCustomerItems”的 UserControl 添加了一个依赖属性,我希望它返回一个 List(仅适用于 IsSelected = true)
  • 此 UserControl 放置在另一个窗口
  • 依赖属性“SelectedCustomerItems”绑定(bind)到 WindowViewModel
  • 内的“SelectedCustomers”属性

    但我没有通过这个依赖属性获得 SelectedCustomer 项目。断点未在 Get{} 中命中 请建议....

    最佳答案

    以这种方式实现您的 DP:

    #region SomeProperty
    /// <summary>
    /// The <see cref="DependencyProperty"/> for <see cref="SomeProperty"/>.
    /// </summary>
    public static readonly DependencyProperty SomePropertyProperty =
        DependencyProperty.Register(
            SomePropertyPropertyName,
            typeof(object),
            typeof(SomeType),
            // other types here may be appropriate for your situ
            new FrameworkPropertyMetadata(null, OnSomePropertyPropertyChanged));
    
    /// <summary>
    /// Called when the value of <see cref="SomePropertyProperty"/> changes on a given instance of <see cref="SomeType"/>.
    /// </summary>
    /// <param name="d">The instance on which the property changed.</param>
    /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
    private static void OnSomePropertyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        (d as SomeType).OnSomePropertyChanged(e.OldValue as object, e.NewValue as object);
    }
    
    /// <summary>
    /// Called when <see cref="SomeProperty"/> changes.
    /// </summary>
    /// <param name="oldValue">The old value</param>
    /// <param name="newValue">The new value</param>
    private void OnSomePropertyChanged(object oldValue, object newValue)
    {
    
    }
    
    /// <summary>
    /// The name of the <see cref="SomeProperty"/> <see cref="DependencyProperty"/>.
    /// </summary>
    public const string SomePropertyPropertyName = "SomeProperty";
    
    /// <summary>
    /// 
    /// </summary>
    public object SomeProperty
    {
        get { return (object)GetValue(SomePropertyProperty); }
        set { SetValue(SomePropertyProperty, value); }
    }
    #endregion  
    

    您必须了解 DependencyProperty 不仅仅是添加了一堆垃圾的属性,它是 的 Hook 。 WPF绑定(bind)系统 .这是一个庞大而复杂的系统,位于海平面以下,您的 DP 优雅地漂浮在其上。它的行为方式出乎你的意料,除非你真的学会了它。

    您正在体验我们所有人对 DP 的第一个启示:绑定(bind)不通过属性访问器(即 getset 方法)访问 DependencyProperty 值。这些属性访问器是您在代码 中使用的便捷方法。仅限 .您可以省略它们并使用 DependencyObject.GetValue and DependencyObject.SetValue ,它们是系统中的实际 Hook (请参阅上面示例中的 getter/setter 的实现)。

    如果您想收听更改通知,您应该按照我在上面的示例中所做的操作。您可以在注册 DependencyProperty 时添加更改通知监听器。您还可以覆盖继承的 DependencyProperties 的“元数据”(我一直为 DataContext 这样做)以添加更改通知(有些人为此使用 DependencyPropertyDescriptor,但我发现它们缺少)。

    但是,无论您做什么,都不要将代码添加到 getset您的 DependencyProperties 的方法! 它们不会被绑定(bind)操作执行。

    有关 DependencyProperties 如何工作的更多信息,I highly suggest reading this great overview on MSDN.

    关于WPF MVVM 依赖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15646766/

    相关文章:

    wpf - 如何在wpf datagrid中显示数据集中的相关数据

    ios - 用于搜索屏幕的 MVVM 和 RxSwift

    .net - 如何继承WPF风格的Button行为?

    c# - 获取所有黑色像素及其在我的 Canvas 上的位置?

    wpf - 从主窗口网格 WPF 中的用户控件打开用户控件

    WPF 绑定(bind) Width 到 Parent.Width*0.3

    c# - 将样式中的 Setter 值绑定(bind)到主模型

    wpf - WPF-在代码中引用应用程序图标

    wpf - MVVM中的数据模板是否过时?

    wpf - 从 View MVVM WPF在 View 模型中定义的访问枚举类型