Advertisement
atsukanrock

Linked Properties in a ViewModel

Apr 4th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. public class ViewModel1 : ViewModelBase
  2. {
  3.     private int _foo;
  4.  
  5.     public int Foo
  6.     {
  7.         get { return _foo; }
  8.         set
  9.         {
  10.             if(value == _foo) return;
  11.  
  12.             _foo = value;
  13.  
  14.             OnPropertyChanged("Foo");
  15.             OnPropertyChanged("Bar");
  16.         }
  17.     }
  18.  
  19.     public int Bar
  20.     {
  21.         get { return _foo * _foo; }
  22.     }
  23. }
  24.  
  25. public class ViewModel2 : ViewModelBase
  26. {
  27.     public ViewModel2()
  28.     {
  29.         PropertyChanged += ViewModel2_PropertyChanged;
  30.     }
  31.  
  32.     private void ViewModel2_PropertyChanged(object sender, PropertyChangedEventArgs e)
  33.     {
  34.         if (e.PropertyName == "Foo")
  35.             OnPropertyChanged("Bar");
  36.     }
  37.  
  38.     private int _foo;
  39.  
  40.     public int Foo
  41.     {
  42.         get { return _foo; }
  43.         set
  44.         {
  45.             if(value == _foo) return;
  46.  
  47.             _foo = value;
  48.  
  49.             OnPropertyChanged("Foo");
  50.         }
  51.     }
  52.  
  53.     public int Bar
  54.     {
  55.         get { return _foo * _foo; }
  56.     }
  57. }
  58.  
  59. public class ViewModel3 : ViewModelBase
  60. {
  61.     private int _foo;
  62.  
  63.     public int Foo
  64.     {
  65.         get { return _foo; }
  66.         set
  67.         {
  68.             if(value == _foo) return;
  69.  
  70.             _foo = value;
  71.  
  72.             OnPropertyChanged("Foo");
  73.             UpdateBar();
  74.         }
  75.     }
  76.  
  77.     private int _bar;
  78.  
  79.     public int Bar
  80.     {
  81.         get { return _bar; }
  82.         private set
  83.         {
  84.             if(value == _bar) return;
  85.  
  86.             _bar = value;
  87.  
  88.             OnPropertyChanged("Bar");
  89.         }
  90.     }
  91.  
  92.     private void UpdateBar()
  93.     {
  94.         Bar = _foo * _foo;
  95.     }
  96. }
  97.  
  98. public class ViewModel4 : ViewModelBase
  99. {
  100.     public ViewModel4()
  101.     {
  102.         PropertyChanged += ViewModel4_PropertyChanged;
  103.     }
  104.  
  105.     private void ViewModel4_PropertyChanged(object sender, PropertyChangedEventArgs e)
  106.     {
  107.         if (e.PropertyName == "Foo")
  108.             UpdateBar();
  109.     }
  110.  
  111.     private int _foo;
  112.  
  113.     public int Foo
  114.     {
  115.         get { return _foo; }
  116.         set
  117.         {
  118.             if(value == _foo) return;
  119.  
  120.             _foo = value;
  121.  
  122.             OnPropertyChanged("Foo");
  123.         }
  124.     }
  125.  
  126.     private int _bar;
  127.  
  128.     public int Bar
  129.     {
  130.         get { return _bar; }
  131.         private set
  132.         {
  133.             if(value == _bar) return;
  134.  
  135.             _bar = value;
  136.  
  137.             OnPropertyChanged("Bar");
  138.         }
  139.     }
  140.  
  141.     private void UpdateBar()
  142.     {
  143.         Bar = _foo * _foo;
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement