2016-08-30 4 views
0

私は四角形を円に変更する方法を実装しようとしています。私が今走ったときに移動できる広場があります。とにかく、どのように四角いボタンのコマンドを実装する必要がありますか?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); 
      } 
     } 
    } 
} 
+0

XAMLから「public ICommand BtnSquareCommand = new RelayCommand();」をバインドすることはできません。あなたはそれを 'public ICommand BtnSquareCommand {get;}に変更する必要があります。私の答えであなたを提供したのと同じように –

+0

あなたはまだ 'public MainViewModel()'の部分がありません... –

答えて

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

はEDIT: あなたはそれが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); 
     } 
    } 
    } 
+0

CanExecuteChangedで何をしますか? –

+0

編集をチェックすると、CanExecuteではなくboolを指定できます。何も指定されていない場合、ボタンは常に有効になります –

+0

アップロードした編集済みのコードを確認してください。自分のコードでどのように表示するように頼むことができますか? –