可取消编辑IEditableObject的使用

<Window x:Class="EditObject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:EditObject"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <vm:ViewModel></vm:ViewModel>
    </Window.DataContext>
    <Grid>
        <StackPanel>
            <TextBlock FontSize="30" Text="Edit"></TextBlock>
            <TextBlock Text="First Name"></TextBlock>
            <TextBox Text="{Binding CurrentEmployee.FirstName}"></TextBox>
            <TextBlock Text="Last Name"></TextBlock>
            <TextBox Text="{Binding CurrentEmployee.LastName}"></TextBox>
            <TextBlock Text="Full Name"></TextBlock>
            <TextBox Text="{Binding CurrentEmployee.FullName, Mode=OneWay}"></TextBox>
            <Button Content="Edit" Command="{Binding EditCommand}"></Button>
            <Button Content="Submit" Command="{Binding SubmitCommand}"></Button>
            <Button Content="Cancel" Command="{Binding CancelCommand}"></Button>

            
        </StackPanel>
       
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EditObject
{
    public class Employee:INotifyPropertyChanged,IEditableObject
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged!=null)
            {
                PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
            }
        }

        private string firstName;
        public string FirstName
        {
            get { return firstName; }
            set
            {
                firstName = value;
                OnPropertyChanged("FirstName");
                OnPropertyChanged("FullName");
            }
        }

        private string lastName;
        public string LastName
        {
            get { return lastName; }
            set
            {
                lastName = value;
                OnPropertyChanged("LastName");
                OnPropertyChanged("FullName");
            }
        }

        private string fullName;
        public string FullName
        {
            get { return FirstName +  " "+LastName; }
          
        }

        private Employee backup;
        

        public void BeginEdit()
        {
            this.backup = this.MemberwiseClone() as Employee; 
        }

        public void CancelEdit()
        {
            this.FirstName = backup.FirstName;
            this.LastName = backup.LastName;
        }

        public void EndEdit()
        {
            
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Helpers
{
    public class RelayCommand : ICommand
    {
        Action _TargetExecuteMethod;
        Func<bool> _TargetCanExecuteMethod;

        public RelayCommand(Action executeMethod)
        {
            _TargetExecuteMethod = executeMethod;
        }

        public RelayCommand(Action executeMethod, Func<bool> canExecuteMethod)
        {
            if (executeMethod == null)
                throw new ArgumentNullException("execute");
            _TargetExecuteMethod = executeMethod;
            _TargetCanExecuteMethod = canExecuteMethod;
        }

        public void RaiseCanExecuteChanged()
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }
        #region ICommand Members

        bool ICommand.CanExecute(object parameter)
        {
            if (_TargetCanExecuteMethod != null)
            {
                return _TargetCanExecuteMethod();
            }
            else
            {
                return true;
            }
            //if (_TargetExecuteMethod != null)
            //{
            //    return true;
            //}
            //return false;
        }

        // Beware - should use weak references if command instance lifetime is longer than lifetime of UI objects that get hooked up to command
        // Prism commands solve this in their implementation
        public event EventHandler CanExecuteChanged = delegate { };

        //public event EventHandler CanExecuteChanged
        //{
        //    add { CommandManager.RequerySuggested += value; }
        //    remove { CommandManager.RequerySuggested -= value; }
        //}

        void ICommand.Execute(object parameter)
        {
            if (_TargetExecuteMethod != null)
            {
                _TargetExecuteMethod();
            }
        }
        #endregion
    }

    public class RelayCommand<T> : ICommand
    {
        Action<T> _TargetExecuteMethod;
        Func<T, bool> _TargetCanExecuteMethod;

        public RelayCommand(Action<T> executeMethod)
        {
            _TargetExecuteMethod = executeMethod;
        }

        public RelayCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
        {
            _TargetExecuteMethod = executeMethod;
            _TargetCanExecuteMethod = canExecuteMethod;
        }

        public void RaiseCanExecuteChanged()
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }
        #region ICommand Members

        bool ICommand.CanExecute(object parameter)
        {
            if (_TargetCanExecuteMethod != null)
            {
                T tparm = (T)parameter;
                return _TargetCanExecuteMethod(tparm);
            }
            if (_TargetExecuteMethod != null)
            {
                return true;
            }
            return false;
        }

        // Beware - should use weak references if command instance lifetime is longer than lifetime of UI objects that get hooked up to command
        // Prism commands solve this in their implementation
        public event EventHandler CanExecuteChanged = delegate { };

        void ICommand.Execute(object parameter)
        {
            if (_TargetExecuteMethod != null)
            {
                _TargetExecuteMethod((T)parameter);
            }
        }
        #endregion
    }
}

using Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace EditObject
{
    public class ViewModel
    {
        public Employee CurrentEmployee { get; set; }
        public ViewModel()
        {
            CurrentEmployee = new Employee{ FirstName="zhang" ,LastName="wenzhong"};
        }

        public RelayCommand EditCommand
        {
            get
            {
                return new RelayCommand(() =>
                    {
                        CurrentEmployee.BeginEdit();
                    });
            }
        }

        public RelayCommand SubmitCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    CurrentEmployee.EndEdit();
                    MessageBox.Show(CurrentEmployee.FullName);
                });
            }
        }

        public RelayCommand CancelCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    CurrentEmployee.CancelEdit();
                });
            }
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/dxm809/article/details/82865899