【WPF】TriggerParameterPathを用いてイベント引数を取得する。
はじめに
WPFでChangeイベント等が発生した際にその値をイベント引数として取得する方法を備忘録として残します。
前提
サンプルコード
以下はコンボボックスの値が変更された際に変更後の値をイベント引数として受け取る際のXAMLです。
<ComboBox>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<prism:InvokeCommandAction
Command="{Binding TestComboBoxChanged}"
TriggerParameterPath="AddedItems"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
ここで記載しているEventName="SelectionChanged"とTriggerParameterPath="AddedItems"が肝となります。
下記のドキュメントの通り、TriggerParameterPathにはSelectionChangedEventArgsプロパティのクラスを記載します。
ViewModel側は以下のように記載することでイベント引数を取得できます。
public MainWindowViewModel() { TestComboBoxChanged = new DelegateCommand<object[]>(TestComboBoxChangedExecute); } public DelegateCommand<object[]> TestComboBoxChanged { get; } // 引数itemsに値が入ってくる private void TestComboBoxChangedExecute(object[] items) { // コンボボックスを変更した際の処理 }
おわりに
以上、TriggerParameterPathを用いてイベント引数を取得する方法でした。