Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 11th, 2012  |  syntax: None  |  size: 1.42 KB  |  hits: 4  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Binding System uses Reflection to find Properties in DataContext then What is the use of Implementing Interface and IOC/DI in ViewModel
  2. namespace ViewModel
  3. {
  4. public class MyViewModel : IMyViewModel, INotifyPropertyChanged
  5. {
  6.     public MyViewModel()
  7.     {
  8.         FirstName = "Harish";
  9.         LastName = "Chauhan";
  10.     }
  11.     private string _firstName;
  12.     private string _lastName;
  13.  
  14.     public string FirstName
  15.     {
  16.         get
  17.         {
  18.             return _firstName;
  19.         }
  20.         set
  21.         {
  22.             _firstName = value;
  23.             Notify("FirstName");
  24.         }
  25.  
  26.     }
  27.     public string LastName
  28.     {
  29.         get
  30.         {
  31.             return _lastName;
  32.         }
  33.         set
  34.         {
  35.             _lastName = value;
  36.             Notify("LastName");
  37.         }
  38.  
  39.     }
  40.  
  41.     #region INotifyPropertyChanged Members
  42.  
  43.     public event PropertyChangedEventHandler PropertyChanged;
  44.  
  45.     #endregion
  46.     private void Notify(string propName)
  47.     {
  48.         if (PropertyChanged != null)
  49.             PropertyChanged(this, new PropertyChangedEventArgs(propName));
  50.     }
  51. }
  52. //Interface
  53. public interface IMyViewModel
  54. {
  55.     string FirstName { get; set; }
  56. }
  57.        
  58. <Grid>
  59.     <Grid.RowDefinitions>
  60.         <RowDefinition Height="70"/>
  61.         <RowDefinition Height="70"/>
  62.     </Grid.RowDefinitions>
  63.     <TextBox x:Name="tbx1" Text="{Binding FirstName, Mode=TwoWay}"/>
  64.     <TextBox x:Name="tbx2" Text="{Binding LastName, Mode=TwoWay}" Grid.Row="1"/>
  65. </Grid>