【问题标题】:BindingList with INotifyPropertyChanged in WPFWPF 中带有 INotifyPropertyChanged 的​​ BindingList
【发布时间】:2015-06-22 18:28:53
【问题描述】:

以下代码示例中的属性“Difference”已更改,以便 UI 获得通知的正确方法是什么?

该属性是只读的。属性的值必须始终基于其他属性计算。

MainWindow.xaml:

<Window x:Name="winCalcs" x:Class="BindingList.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:m="clr-namespace:BindingList"
        Title="Calculations" Height="350" Width="525">
    <Window.Resources>
        <m:OperationList x:Key="OperationData"/>
        <CollectionViewSource x:Key="Operations" 
                              Source="{StaticResource ResourceKey=OperationData}"/>
    </Window.Resources>

    <Grid>
        <TabControl x:Name="tabsMain">
            <TabItem x:Name="tab01" Header="Tab 1">
                <DataGrid x:Name="dg01"
                          ItemsSource="{Binding 
                    Source={StaticResource ResourceKey=Operations}, 
                    UpdateSourceTrigger=PropertyChanged}" />
            </TabItem>
            <TabItem x:Name="tab02" Header="Tab 2">
                <DataGrid x:Name="dg02" 
                          ItemsSource="{Binding 
                    Source={StaticResource ResourceKey=Operations}, 
                    UpdateSourceTrigger=PropertyChanged}" />
            </TabItem>
        </TabControl>
    </Grid>

</Window>

Operation.cs:

namespace BindingList
{
    class Operation : INotifyPropertyChanged 
    {
        private float _minuend;

        private float _subtrahend;

        public float Minuend
        {
            get
            {
                return this._minuend;
            }
            set
            {
                if (this._minuend == value) return;
                this._minuend = value;
                this.NotifyPropertyChanged("Minuend");
            }
        }

        public float Subtrahend
        {
            get
            {
                return this._subtrahend;
            }
            set
            {
                if (this._subtrahend == value) return;
                this._subtrahend = value;
                this.NotifyPropertyChanged("Subtrahend");
            }
        }

        public float Difference 
        {
            get
            {
                return Minuend - Subtrahend;
            }
            private set {}
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string p)
        {
            if (PropertyChanged != null) 
                PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }
}

OperationList.cs:

namespace BindingList
{
    class OperationList : BindingList<Operation>
    {
        public OperationList()
        {
            Add(new Operation());
        }
    }
}

【问题讨论】:

  • 只需在MinuendSubtrahend 属性中添加this.NotifyPropertyChanged("Difference ");
  • 请注意,只读属性不需要private set {}。根本不要写 setter。

标签: c# wpf inotifypropertychanged bindinglist observablelist


【解决方案1】:

在这些情况下,我通常会显式设置属性并引发PropertyChanged 事件。

namespace BindingList
{
    class Operation : INotifyPropertyChanged
    {
        private float _minuend;
        private float _subtrahend;
        private float _difference;
        public float Minuend
        {
            get
            {
                return this._minuend;
            }

            set
            {
                if (this._minuend == value)
                    return;
                this._minuend = value;
                this.NotifyPropertyChanged("Minuend");
                this.UpdateDifference();
            }
        }

        public float Subtrahend
        {
            get
            {
                return this._subtrahend;
            }

            set
            {
                if (this._subtrahend == value)
                    return;
                this._subtrahend = value;
                this.NotifyPropertyChanged("Subtrahend");
                this.UpdateDifference();
            }
        }

        private void UpdateDifference()
        {
            this.Difference = this.Minuend - this.Subtrahend;
        }

        public float Difference
        {
            get
            {
                return this._difference
            }

            private set
            {
                if (this._difference == value)
                    return;
                this._difference = value;
                this.NotifyPropertyChanged("Difference");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string p)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }
}

【讨论】:

    【解决方案2】:

    会随着被减数减数改变而改变。这意味着您需要通知 MuendSubtrahend 集合内的 Difference 发生变化。

    Difference 不需要属性设置器。

    附带说明,没有必要在任何地方都使用this

    public float Minuend
    {
        get
        {
            return _minuend;
        }
        set
        {
            if (_minuend == value) return;
            _minuend = value;
            NotifyPropertyChanged("Minuend");
            NotifyPropertyChanged("Difference");
        }
    }
    
    public float Subtrahend
    {
        get
        {
            return _subtrahend;
        }
        set
        {
            if (_subtrahend == value) return;
            _subtrahend = value;
            NotifyPropertyChanged("Subtrahend");
            NotifyPropertyChanged("Difference");
        }
    }
    
    public float Difference 
    {
        get
        {
            return Minuend - Subtrahend;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-28
      • 2013-06-07
      • 2011-12-25
      • 2021-09-07
      • 2017-05-25
      • 2017-09-19
      • 1970-01-01
      相关资源
      最近更新 更多