我正在尝试实现一种方法,它可以将一个正方形图转换成一个圆。当我现在跑的时候,有一个正方形可以移动。无论如何,应该如何实现平方按钮的命令?
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);
}
}
}
}发布于 2016-08-30 12:03:25
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);
}
}
}https://stackoverflow.com/questions/39226858
复制相似问题