Sample MVVM Driven WPF Application

WPF has a very powerful data binding feature, that provides an easy one-way or two-way synchronization of properties.

The model-view-viewmodel is a typically WPF pattern. It consists of a view, that gets all the user input and forwards it to the viewmodel, typically by using commands.
 
This is a pattern created by by John Gossman from WPF team.
Below is the sample application which shows up how to implement the WPF PropertyChanged trigger.
 
 XAML implementation

    <DockPanel Margin=”10″>
        <StackPanel DockPanel.Dock=”Right” Margin=”10,0,0,0″>
            <Button Name=”btnAddUser” Click=”btnAddUser_Click”>Add Employee</Button>
            <Button Name=”btnChangeUser” Click=”btnChangeUser_Click” Margin=”0,5″>Change Employee</Button>
            <Button Name=”btnDeleteUser” Click=”btnDeleteUser_Click”>Delete Employee</Button>
        </StackPanel>
        <ListBox Name=”lbUsers” DisplayMemberPath=”Age”></ListBox>
    </DockPanel>

CodeBehind Implementation

 public partial class MainWindow : Window     {      

        private ObservableCollection<Employee> employees = new ObservableCollection<Employee>();

 

        public  MainWindow()                 {

                    InitializeComponent();                        employees.Add(new Employee() { Name = “Aieshwanth Deepak ” ,Age =18});                        employees.Add(new Employee() { Name = “Sharika Shivani”, Age = 19 });                        employees.Add(new Employee() { Name = “Jeeva Rekha”, Age = 28 });

                        lbUsers.ItemsSource = employees;                 }

                private void btnAddEmployee_Click(object sender, RoutedEventArgs e)                 {                         employees.Add(new Employee() { Name = “New Employee” ,Age=25});                 }

                private void btnChangeEmployee_Click(object sender, RoutedEventArgs e)                 {                         if(lbUsers.SelectedItem != null)                         {                                 (lbUsers.SelectedItem as Employee).Name = “Raymond Schenk”;                                 (lbUsers.SelectedItem as Employee).Age = 48;                         }

                }

                private void btnDeleteEmployee_Click(object sender, RoutedEventArgs e)                 {                         if(lbUsers.SelectedItem != null)                                 employees.Remove(lbUsers.SelectedItem as Employee);                 }         }

        public class Employee : INotifyPropertyChanged         {                 private string name;                 public string Name {                         get { return this.name; }                         set                         {                                 if(this.name != value)                                 {                                         this.name = value;                                         this.NotifyPropertyChanged(“Name”);                                 }                         }                 }

                public event PropertyChangedEventHandler PropertyChanged;

                public int _age;

                public int Age                 {                     get { return this._age; }                     set                     {                         if (this._age != value)                         {                             this._age = value;                             this.NotifyPropertyChanged(“Age”);                         }                     }                 }                 public void NotifyPropertyChanged(string propName)                 {                         if(this.PropertyChanged != null)                                 this.PropertyChanged(this, new PropertyChangedEventArgs(propName));                 }         }

 

Hope this helps

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s