###前提・実現したいこと
C#にてWPF+GalaSoft.MvvmLightでアプリを開発しています。
DataGrid上のセルにButtonを配置してクリックイベントでクリックされた行の情報を取得したいと考えています。
現在GalaSoft.MvvmLightライブラリのメソッドEventToCommandでメソッドをバインドしていますが、実現できていません。
EventToCommandによる実装方法、および他の実装方法による実現も含めて検討しています。
###発生している問題・エラーメッセージ
GalaSoft.MvvmLightライブラリのメソッドEventToCommandで、DataGrid上のセルに配置したButtonのクリックイベントをViewModel上のメソッドにバインドしていますが、バインド先のメソッドが実行されません。
PageのLoadイベントにバインドされているメソッドは実行されているのでコードミスによる実行は考え難いと思います。
###該当のソースコード
Xamlソース
(略)
↓ ViewModel上のイベントバインドしたメソッドが実行される
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<cmd:EventToCommand Command="{Binding PageMemberListLoadedCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
↑ ViewModel上のイベントバインドしたメソッドが実行される
(略)
<DataGridTemplateColumn x:Name="ColName" Width="150">
<DataGridTemplateColumn.Header>
<Button x:Name="ColNameTitleButton" Content="名前" Style="{StaticResource TitleButton}" Width="150">
↓ ViewModel上のイベントバインドしたメソッドが実行されない
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding ButtonClickCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
↑ ViewModel上のイベントバインドしたメソッドが実行されない
</Button>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="btnName" HorizontalContentAlignment="Left" Style="{StaticResource CellButton}" Width="150">
↓ ViewModel上のイベントバインドしたメソッドが実行されない
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding ButtonClickCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
↑ ViewModel上のイベントバインドしたメソッドが実行されない
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
ViewModelソース
↓ ViewModel上の実行されるメソッド
private ICommand _PageMemberListLoadedCommand;
public ICommand PageMemberListLoadedCommand
{
get
{
return _PageMemberListLoadedCommand ?? (_PageMemberListLoadedCommand = new RelayCommand<RoutedEventArgs>(PageMemberListLoaded));
}
}public void PageMemberListLoaded(RoutedEventArgs e) {
(略)
}
↑ ViewModel上の実行されるメソッド↓ ViewModel上の実行されないメソッド
private ICommand _ButtonClickCommand;
public ICommand ButtonClickCommand
{
get
{
return _ButtonClickCommand ?? (_ButtonClickCommand = new RelayCommand<RoutedEventArgs>(ButtonClick));
}
}public void ButtonClick(RoutedEventArgs e) { try { MessageBox.Show("ButtonClick"); } catch { } }
↑ ViewModel上の実行されないメソッド
###試したこと EventToCommandによる実装方法の調査を行いましたが、実現できる実装方法は見つかっていません。 実行されないイベントバインド箇所(DataGrid上に配置したButtonのクリックイベント)のはバインドしない形で記述すると<EventSetter>を使用して記述する必要があると思います。EventSetterを使用してイベントメソッドをフックする場合にEventToCommandでは実行されないとの情報を得て、他の実装方法についても調査検討したのですが、実装方法が見つかっていません。 ###補足情報(言語/FW/ツール等のバージョンなど) 言語 C# FW .NetFramework4.5.2 ライブラリ GalaSoft.MvvmLight 5.3 統合環境 VS2015
回答1件
あなたの回答
tips
プレビュー