Thursday, 22 July 2010

Silverlight MVVM Setup

Property
1.       Copy ViewModelBase.cs
2.       Create this property on View Model .cs file
         
private String _backgroundColour;
          public String BackgroundColour
          {
                     get { return _backgroundColour; }
                     set
                     {
                         _backgroundColour = value;
                         OnPropertyChanged("BackgroundColour");
                     }
          }
3.       Assign this property somewhere in View Model
BackgroundColour = "#FFAA2323";
4.       Bind the property on xaml file
Background="{Binding Path=BackgroundColour}"


Command
1.       Copy RelayCommand.cs
2.       Create a command variable on View Model .cs file
                    private readonly RelayCommand _paintBox;
          public ICommand PaintBox { get { return _paintBox; } }
3.       Create a method on View Model .cs file
         
public void PaintBoxWithColour()
          {
                     …
          }
4.       Initiate the command variable in View Model constructor
          _paintBox = new RelayCommand(PaintBoxWithColour);
5.       Also make sure that is enabled
          _paintBox.IsEnabled = true;
6.       Bind this command on xaml file
          … Command="{Binding Path=PaintBox}"