WPF(C#)でCommadプロパティでBindingした処理が実行されない

開発環境:
VisualSTudio2019
WPF(.netframework)

質問
MVVMパターンToDoAppのようなものを開発しているのですが、
やりたいこととして、DataGridタグにて行ごとにボタンを表示して、そのボタンをクリックしたら何か処理を実行したいのですが、
以下の、 Debug.WriteLine("実行ボタンが押されました"); が実行されず何も処理が実行されませんでした。

DataGridの外に書いたButtonタグは実行されたので、DataTemplateタグの中に書いてあることが問題なのでしょうか?

<MainView.xaml>

コード <DataGrid Grid.Column="1" ItemsSource="{Binding Customer}" AutoGenerateColumns="False" x:Name="DataGrid1" VirtualizingPanel.ScrollUnit="Pixel" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" SelectionUnit="CellOrRowHeader"> <DataGrid.Columns> <DataGridTemplateColumn Header="実行ボタン"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Content="Act" Command="{Binding ActBtn}" Tag="{Binding}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>

MainViewModel.cs

コード private DelegateCommand _actBtn; public DelegateCommand ActBtn { get { return this._actBtn ?? (this._actBtn = new DelegateCommand( _ => { Debug.WriteLine("実行ボタンが押されました"); } )); } }

DelegateCommnad.cs

コード using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfTaskMindSystem { using System.Windows.Input; internal class DelegateCommand : ICommand { /// <summary> /// この2つはデリゲートつまり、methodが登録しておける入れ物、so,要所要所でNullとかでるのは /// 登録されてなかったらこのでりげーと実行する必要がないからそこで条件式かいてるのかな?エラー出るのかな? /// </summary> private Action<object> _execute; private Func<object, bool> _canExecute; /// <summary> /// コンストラクタ /// </summary> /// <param name="execute"></param> public DelegateCommand(Action<object> execute) : this(execute, null) { } /// <summary> /// /// </summary> /// <param name="execute"></param> /// <param name="canExecute"></param> public DelegateCommand(Action<object> execute, Func<object, bool> canExecute) { this._execute = execute; this._canExecute = canExecute; } public bool CanExecute(object parameter) { return (this._canExecute != null) ? this._canExecute(parameter) : true; } public event EventHandler CanExecuteChanged; public void RaiseCanExecuteChanged() { var h = this.CanExecuteChanged; if (h != null) h(this, EventArgs.Empty); } public void Execute(object parameter) { if (this._execute != null) this._execute(parameter); } } }

コメントを投稿

0 コメント