2017-11-13 9 views
1

モデルの拡張に問題があります。 Stationクラスで更新されたプロパティAngleと同じ時間にAngleNegativeプロパティを更新する必要があります。 Angleからのデータに基づくAngleNegativeプロパティデータです(例で見ることができます)。私はステーションモデルを変更することはできません。なぜなら、それは別のVMで使用され、同じ問題を持ちますが、別のプロパティを持つからです。これの解決方法は何ですか?ViewModelの別のプロパティでモデル内のプロパティの変更をキャッチするにはどうすればよいですか?

public class Station : BindableBase, IStation 
    { 
     //some properties 
     . 
     . 
     . 

     private int _angle; 

     public int Angle 
     { 
      get => _angle; 
      set => SetProperty(ref _angle, value); 
     } 
    } 


public class ViewModel : BindableBase 
    { 
     private Station _station; 
     public Station Station 
     { 
      get => _station; 
      set => SetProperty(ref _station, value); 
     } 

     //delete this property duplicate and base on Station.Angle 
     private int _angle; 
     public int Angle 
     { 
      get => _angle; 
      set 
      { 
       SetProperty(ref _angle, value); 
       AngleNegative = value - 180; 
      } 
     } 

     private int _angleNegative; 

     public int AngleNegative 
     { 
      get => _angleNegative; 
      set => SetProperty(ref _angleNegative, value); 
     } 
    } 

私はIStationからVMを継承することができますが、コードの多くは後で複製されると思います。

答えて

0

私はこのようにそのStationPropertyChangedイベントをリッスンViewModelに上の方法持つことによってそれを行うだろう:

public class ViewModel : BindableBase 
{ 
    private Station _station; 
    public Station Station 
    { 
     get => _station; 
     set => SetProperty(ref _station, value); 
    } 

    private int _angleNegative; 
    public int AngleNegative 
    { 
     get => _angleNegative; 
     set => SetProperty(ref _angleNegative, value); 
    } 

    public ViewModel() 
    { 
     Station.PropertyChanged += Station_PropertyChanged; 
    } 

    private void Station_PropertyChanged(object sender, PropertyChangedEventArgs e) 
    { 
     if (e.PropertyName == "Angle") 
     { 
      AngleNegative = Station.Angle - 180; 
     } 
    } 
} 
+0

はい、その良いアイデアを!私はそれをしようとすると、それは駅のプロパティ(私はプロパティにオブジェクトをリンクする)のセットで購読する必要があります。ありがとう! –

+0

あなたは大歓迎です! –

関連する問題