BindingList与WPF中的INotifyPropertyChanged

时间:2015-04-16 11:22:15

标签: c# wpf inotifypropertychanged bindinglist observablelist

用于获取通知的用户界面的正确方法是什么? 差异 "在以下代码示例中已更改?

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

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());
        }
    }
}

2 个答案:

答案 0 :(得分:3)

Minuend Subtrahend 更改时,

差异会发生变化。这意味着您需要在 Minuend Subtrahend 的集合中通知已更改差异

不需要为差异设置属性。

另一方面,无需在任何地方使用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;
    }
}

答案 1 :(得分:0)

在这些情况下,我通常会明确设置属性并引发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));
        }
    }
}