所谓的潇洒

导航

WPF知识点备忘录——MVVM原理

通知类

namespace WPFMVVM.ViewModels
{
    class NotificationObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void RasePropertyChanged(string propertyName)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
View Code

命令类

namespace WPFMVVM.Commands
{
    class DelegateCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            if (CanExecuteFunc == null) return true;
            return this.CanExecuteFunc(parameter);
        }

        public void Execute(object parameter)
        {
            if (ExecuteAction == null) return;
            this.ExecuteAction(parameter);
        }

        public Action<object> ExecuteAction { get; set; }
        public Func<object,bool> CanExecuteFunc { get; set; }
    }
}
View Code

View

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Button Grid.Column="1" Grid.Row="1" HorizontalAlignment="Right" Content="Add" Command="{Binding AddCommand}"/>
        <Button Grid.Column="2" Grid.Row="1" HorizontalAlignment="Right" Content="Edit" 
                Command="{Binding EditCommand}" CommandParameter="{Binding ElementName=dataGrid,Path=SelectedItem}"/>

        <DataGrid Grid.ColumnSpan="3" Name="dataGrid" Margin="3" Grid.Row="2"
                  Padding="3" ItemsSource="{Binding People}" Grid.RowSpan="3" />
    </Grid>
View Code

CS代码

    public partial class BindingAndValidation : Window
    {
        public BindingAndValidation()
        {
            InitializeComponent();
            this.DataContext = new BindingAndValidationViewModel();
        }
    }

ViewModel

namespace WPFMVVM.ViewModels
{
    class BindingAndValidationViewModel : NotificationObject
    {
        private ObservableCollection<PersonModel> people = new ObservableCollection<PersonModel>();
        public ICollection<PersonModel> People
        {
            get { return people; }
            set
            {
                people = (ObservableCollection<PersonModel>)value;
                this.RasePropertyChanged("People");
            }
        }

        public DelegateCommand AddCommand { get; set; }
        public DelegateCommand DelCommand { get; set; }
        public DelegateCommand EditCommand { get; set; }

        private void Add(object parameter)
        {
            var dlg = new PersonEdit(null);
            if (dlg.ShowDialog() == true)
            {
                var mod = (PersonViewModel)dlg.DataContext;
                people.Add(new PersonModel { ID = mod.ID, Name = mod.Name });
            }
        }

        private void Edit(object parameter)
        {
            var mod = (PersonModel)parameter;
            var dlg = new PersonEdit(mod);
            if (dlg.ShowDialog() == true)
            {
                var person = people.FirstOrDefault(x => x.ID == mod.ID);
                person.Name = mod.Name;
            }
        }

        public BindingAndValidationViewModel()
        {
            people.Add(new PersonModel { ID = 1, Name = "A" });
            people.Add(new PersonModel { ID = 2, Name = "B" });
            people.Add(new PersonModel { ID = 3, Name = "C" });

            AddCommand = new DelegateCommand();
            AddCommand.ExecuteAction = new Action<object>(this.Add);

            EditCommand = new DelegateCommand();
            EditCommand.ExecuteAction = new Action<object>(this.Edit);
        }  
    }
}
View Code

 

posted on 2023-02-28 23:11  所谓的潇洒  阅读(59)  评论(0编辑  收藏  举报