实现BtnSquareCommand / RelayCommand

时间:2016-08-30 11:56:44

标签: .net button relaycommand

我正在尝试实现一种方法,可以将方形图形更改为圆形。当我现在跑步时,有一个可以移动的方块。无论如何,如何实现方形按钮的命令?

namespace Square
{
    public class MainViewModel : INotifyPropertyChanged
    {

        private double _x;

        public object Content { get; set; }

        public double X
        {
            get { return _x; }
            set
            {
                _x = value;
            }
        }

        public ICommand BtnSquareCommand = new RelayCommand(); //I'm stuck here

        public double Y { get; set; }

        public MainViewModel()
        {
            Content = new SquareViewModel();
        }



        private void SetSquare()
        {
            Content = new SquareViewModel();
        }
        private void SetCircle()
        {
            Content = new SquareViewModel();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnProperrtyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}

编辑:

按钮已经实现但不是它们的功能,所以它们什么都不做

namespace Square
{

    public class MainViewModel : INotifyPropertyChanged
    {

        private double _x;

        public object Content { get; set; }

        public double X
        {
            get { return _x; }
            set
            {
                _x = value;
            }
        }

        public ICommand BtnSquareCommand {get ; set;} 
        public ICommand BtnCircleCommand {get ; set;} 


        public double Y { get; set; }

        public MainViewModel()
        {
            Content = new SquareViewModel();
        }



        private void SetSquare()
        {
            Content = new SquareViewModel();
        }
        private void SetCircle()
        {
            Content = new CircleViewModel();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnProperrtyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

        }

    }
}

此后我该怎么办?

...

namespace Sqaure
{
    public class MainViewModel : INotifyPropertyChanged
    {

        private double _x;

        public object Content { get; set; }

        public double X
        {
            get { return _x; }
            set
            {
                _x = value;
                OnPropertyChanged("Content");
            }
        }

        public ICommand BtnSquareCommand { get; set; }
        void BtnSquareCommand_Click(object obj)
        {
            SetSquare();
        }

        public ICommand BtnCircleCommand { get; set; }
        void BtnCircleCommand_Click(object obj)
        {
            SetCircle();
        }

        public double Y { get; set; }

        public MainViewModel()
        {
            Content = new SquareViewModel();
            BtnSquareCommand = new RelayCommand(BtnSquareCommand_Click); 
        }



        private void SetSquare()
        {
            Content = new SquareViewModel();
        }
        private void SetCircle()
        {
            Content = new CircleViewModel();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            Console.WriteLine("HEEJ");

        }

        public class RelayCommand : ICommand
        {
            readonly Action<object> _execute;
            readonly Func<bool> _canExecute;

            public RelayCommand(Action<object> execute, Func<bool> canExecute = null)
            {
                if (execute == null)
                    throw new ArgumentNullException(nameof(execute));

                _execute = execute;
                _canExecute = canExecute;
            }

            public bool CanExecute(object parameter)
            {
                return _canExecute == null || _canExecute.Invoke();
            }

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

            public void Execute(object parameter)
            {
                _execute(parameter);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

public MainViewModel()
{
   Content = new SquareViewModel(); 
   BtnSquareCommand= new RelayCommand(BtnSquareCommand_Click, ()=> CanExecute); //I'm stuck here

}

public ICommand BtnSquareCommand {get ; set;} 

void BtnSquareCommand_Click(object obj)
{
  //DO YOUR WORK HERE
}

检查我对同一事项的其他答案。有关如何正确执行的更多解释: Create a SearchCommand in wpf using Mvvm that loads new data to my list view

修改 您还必须更改内容部分,它需要实现INotifyPropertyChanged;

 private object _content;
 public object Content
 {
   get{return _content;}
   set
     { 
       _content = value; 
        OnPropertyChanged("Content"");
      }
 }

编辑编辑:

我要为你发布全班。

只需将其粘贴到您的项目中即可。

namespace Sqaure
{
  public class MainViewModel : INotifyPropertyChanged
  {
    private double _x;
    private object _content;
    public object Content 
    { 
       get{return _content;}
       set
       {
          _content = value;
          OnPropertyChanged("Contenct");
       }
    }

    public double X
    {
        get { return _x; }
        set
        {
            _x = value;
            OnPropertyChanged("X");
        }
    }

    public ICommand BtnSquareCommand { get; set; }

    void BtnSquareCommand_Click(object obj)
    {
        SetSquare();
    }

    public ICommand BtnCircleCommand { get; set; }
    void BtnCircleCommand_Click(object obj)
    {
        SetCircle();
    }

    public double Y { get; set; }

    public MainViewModel()
    {
        Content = new SquareViewModel();
        BtnSquareCommand = new RelayCommand(BtnSquareCommand_Click); 
        BtnCircleCommand = new RelayCommand(BtnCircleCommand_Click);
    }

    private void SetSquare()
    {
        Content = new SquareViewModel();
    }
    private void SetCircle()
    {
        Content = new CircleViewModel();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        Console.WriteLine("HEEJ");

    }
}

 public class RelayCommand : ICommand
    {
        readonly Action<object> _execute;
        readonly Func<bool> _canExecute;

        public RelayCommand(Action<object> execute, Func<bool> canExecute = null)
        {
            if (execute == null)
                throw new ArgumentNullException(nameof(execute));

            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute == null || _canExecute.Invoke();
        }

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

        public void Execute(object parameter)
        {
            _execute(parameter);
        }
    }
  }