【问题标题】:Check if value actually changed in WPF property setter?检查 WPF 属性设置器中的值是否实际更改?
【发布时间】:2012-09-27 09:10:06
【问题描述】:

在 WPF 中,是否最好在调用 NotifyPropertyChanged 之前检查模型属性的值是否会在属性设置器中实际更改?

例如:

    private string foobar;

    public string Foobar
    {
        get
        {
            return this.foobar;
        }

        set
        {
            if (value != this.foobar)
            {
                this.foobar = value;
                this.NotifyPropertyChanged("Foobar");
            }
        }
    }

另一种方法是不检查,每次都调用 NotifyPropertyChanged:

    private string foobar;

    public string Foobar
    {
        get
        {
            return this.foobar;
        }

        set
        {
            this.foobar = value;
            this.NotifyPropertyChanged("Foobar");
        }
    }

我已经在代码示例中看到了这两种样式。每种方法的优缺点是什么?

【问题讨论】:

标签: .net wpf


【解决方案1】:

我总是进行检查,因为该事件似乎暗示应该发生实际变化。

(还可以防止不必要的更新)

【讨论】:

    猜你喜欢
    • 2017-02-27
    • 2017-12-07
    • 2018-10-01
    • 2015-10-17
    • 2013-06-14
    • 1970-01-01
    • 2014-05-05
    • 2023-03-09
    • 2011-06-30
    相关资源
    最近更新 更多