13/08/13

Add MVVM

Mainwindow file
<Window x:Class="AddressBookWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Command="{Binding add}" Content="Add" Height="23" HorizontalAlignment="Left" Margin="51,108,0,0" Name="btnAdd" VerticalAlignment="Top" Width="75" />
        <Button Command="{Binding update}" Content="Update" Height="23" HorizontalAlignment="Left" Margin="151,108,0,0" Name="btnupdate" VerticalAlignment="Top" Width="75" />
       
        <Label Content="Name:" Height="28" HorizontalAlignment="Left" Margin="141,11,0,0" Name="label1" VerticalAlignment="Top" />
        <TextBox Text="{Binding Name}" Height="23" HorizontalAlignment="Left" Margin="193,13,0,0" Name="textBox1" VerticalAlignment="Top" Width="181" />
        <Label Content="Address:" Height="28" HorizontalAlignment="Left" Margin="141,40,0,0" Name="label2" VerticalAlignment="Top" />
        <TextBox Text="{Binding Address}" Height="23" HorizontalAlignment="Left" Margin="193,42,0,0" Name="textBox2" VerticalAlignment="Top" Width="181" />
        <Label Content="E-mail:" Height="28" HorizontalAlignment="Left" Margin="141,69,0,0" Name="label3" VerticalAlignment="Top" />
        <TextBox Text="{Binding Email}" Height="23" HorizontalAlignment="Left" Margin="193,71,0,0" Name="textBox3" VerticalAlignment="Top" Width="181" />
        <DataGrid AutoGenerateColumns="False" Height="213" HorizontalAlignment="Left" Margin="12,154,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Name}" Header="Name" MinWidth="40" Width="100" />
                <DataGridTextColumn Binding="{Binding PersonAddress}" Header="Address" Width="230" />
                <DataGridTextColumn Binding="{Binding Email}" Header="E-mail" Width="140" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

 public partial class MainWindow : Window
    {
        public MainWindowViewModel ViewModel;
        public MainWindow()
        {
            InitializeComponent();
            ViewModel=new MainWindowViewModel();
            this.DataContext = ViewModel;
            dataGrid1.ItemsSource = ViewModel.Entries;
        }
    }

create class
public class AddCommand:ICommand
    {
        private readonly MainWindowViewModel _vm;
        public  AddCommand(MainWindowViewModel vm)
        {
            _vm = vm;

        }

        public void Execute(object parameter)
        {
            _vm.Add();
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;
    }

then also
public class Address
    {
        public Address()
        {
        }

        public string Name { get; set; }
        public string PersonAddress { get; set; }
        public string Email { get; set; }
    }

last one view model
 public class MainWindowViewModel: DependencyObject

    {
        public ObservableCollection<Address> Entries=new ObservableCollection<Address>();
        public ICommand add { get; set; }
        public ICommand update { get; set; }

        #region  //dependency properties section
        public static readonly DependencyProperty NameProperty =
            DependencyProperty.Register("Name", typeof (string), typeof (MainWindowViewModel), new PropertyMetadata(default(string)));

        public string Name
        {
            get { return (string) GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        public  static readonly DependencyProperty AddressProperty =
            DependencyProperty.Register("Address", typeof (string), typeof (MainWindowViewModel), new PropertyMetadata(default(string)));

        public string Address
        {
            get { return (string) GetValue(AddressProperty); }
            set { SetValue(AddressProperty, value); }
        }

        public static readonly DependencyProperty EmailProperty =
            DependencyProperty.Register("Email", typeof (string), typeof (MainWindowViewModel), new PropertyMetadata(default(string)));

        public string Email
        {
            get { return (string) GetValue(EmailProperty); }
            set { SetValue(EmailProperty, value); }
        }
        #endregion

       
        public MainWindowViewModel()
        {
            add=new AddCommand(this);
           

        }
        public void Add()
        {
            Entries.Add(new Address(){Email = this.Email,Name = this.Name,PersonAddress = this.Address});
            Name = "";
            Address = "";
            Email = "";
        }      
    }

No comments:

Post a Comment