【问题标题】:Wpf binding Clarification (beginner)Wpf绑定说明(初学者)
【发布时间】:2014-09-29 11:07:05
【问题描述】:

正如标题所示,我是 wpf 的新手。我一直在使用 wpf,因为它是 winforms(无论如何,这都是什么绑定的无意义的东西),当然,直到我尝试了它并被吹走了。

所以我正在深入研究用户控件和依赖属性。我读到了,为了让 ui 与引擎盖下的内容保持同步,您需要为您使用的东西使用可观察集合、notifypropertychanged / 更改和依赖属性。

我的问题是:

假设我有这个部门。支柱。对于用户控件(Media.Color 类型):

public Color Color
{
    get { return (Color)GetValue(ColorProperty); }
    set { SetValue(ColorProperty, value); }
}

xaml 将它用于绑定,它可以工作,一切都很好。但是,当它更新时,我想在代码中对其进行处理。

所以我尝试像这样放置一个 Console.writeline("fired"):

public Color Color
{
    get { return (Color)GetValue(ColorProperty); }
    set { 
            Console.WriteLine("Fired"); 
            SetValue(ColorProperty, value); 
        }
}   

没有骰子。有人可以告诉我这些东西是如何工作的吗?我显然遗漏了一些东西(就在前几天有人在堆栈上告诉我关于 MouseCapture 所以......)。

感谢您的宝贵时间。

编辑

http://www.wpftutorial.net/DependencyProperties.html

基本上是用粗体大写的,

重要提示:不要向这些属性添加任何逻辑,因为它们是 仅在您从代码设置属性时调用。如果你设置 XAML 中的属性直接调用 SetValue() 方法。

如果您使用的是 Visual Studio,您可以输入 propdp 并点击 2x tab 以 创建一个依赖属性。

然后继续解释为什么以及如何继续。

解决方案

所以,我尝试了@Krishna 的建议,但我的用户控件崩溃并烧毁。

这是我的 dep 道具。 (就像在问这个问题之前一样)。

public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(ColorPickerMaster), new PropertyMetadata(default(Color)));

原来问题出在使用 (...) new Prop.Metadata(null, OnPropChanged)

使用

public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(ColorPickerMaster), new PropertyMetadata(OnColorChanged));

private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    Console.WriteLine(e.NewValue);
}

带来了一场美丽的胜利。

感谢您的时间和回答。

【问题讨论】:

标签: c# wpf binding dependency-properties


【解决方案1】:

当涉及到 DependencyProperties 时,您可以使用属性更改回调来跟踪对您的属性的更改,如下例所示。然后你使用 e.NewValue 和 e.OldValue 来编写你的逻辑。更多关于 MSDN 上的 DependencyProperties 的信息

public Color color
        {
            get { return (Color)GetValue(colorProperty); }
            set { SetValue(colorProperty, value); }
        }
        public static readonly DependencyProperty colorProperty =
            DependencyProperty.Register("color", typeof(Color), typeof(YourClass), new PropertyMetadata(null,colorChanged));

        private static void colorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            YourClass c = d as YourClass;
            if(c!=null)
            {

            }
        }

【讨论】:

    【解决方案2】:

    来自 MSDN - XAML Loading and Dependency Properties:

    其 XAML 处理器的当前 WPF 实现本质上是依赖属性感知的。 WPF XAML 处理器在加载二进制 XAML 和处理作为依赖属性的属性时使用属性系统方法来处理依赖属性。 这有效地绕过了属性包装器。当你实现自定义依赖属性时,你必须考虑到这种行为,并且应该避免在你的属性包装器中放置除属性系统方法 GetValue 和 SetValue 之外的任何其他代码。

    如果您想在 setter 中添加自定义逻辑,您必须使其成为一个简单字段(不是 DependecyProperty)实现 INotifyPropertyChanged 并绑定到它。

    【讨论】:

    • 用于用户控件的 ui。因此依赖属性。如果我尝试其他方式,xaml 会哭。天哪,我好困惑。
    猜你喜欢
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 2011-04-05
    • 2010-10-28
    • 2016-12-02
    相关资源
    最近更新 更多