私は四角形を円に変更する方法を実装しようとしています。私が今走ったときに移動できる広場があります。とにかく、どのように四角いボタンのコマンドを実装する必要がありますか?BtnSquareCommand/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));
}
}
}
EDIT:
ボタンがすでに実装ではなく、その機能をので、彼らが
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);
}
}
}
}
XAMLから「public ICommand BtnSquareCommand = new RelayCommand();」をバインドすることはできません。あなたはそれを 'public ICommand BtnSquareCommand {get;}に変更する必要があります。私の答えであなたを提供したのと同じように –
あなたはまだ 'public MainViewModel()'の部分がありません... –