2015-04-16 11 views
1

Что такое правильный способ для пользовательского интерфейса, чтобы получить уведомление о том, что свойство «разница» изменилось в следующем примере кода?BindingList с INotifyPropertyChanged в WPF

Имущество доступно только для чтения. Значение свойства всегда должно рассчитываться на основе других свойств.

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

просто добавьте 'this.NotifyPropertyChanged («разница»),' 'в обоих Minuend' и' Subtrahend' свойство – Franck

+0

Как примечание, вам не нужно 'частный набор {}' для чтения только имущество. Просто не пишите сеттер вообще. – Clemens

ответ

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; 
    } 
} 
0

В таких ситуациях я обычно явно задано свойство и и поднять PropertyChanged событие.