LoginSignup
13
13

More than 3 years have passed since last update.

【C#/WPF】EventTriggerを使って、Buttonでなくてもクリック時のCommandをかけるようにする

Posted at

やりたいこと

Buttonの「Command」プロパティに、ViewModelのICommandに書いた処理をバインドして「ボタンを押したときの処理」を書いていたが、Button以外の普通のコントロール(例えばGridとか)に、「押したときの処理」を書きたい。

=ViewModelで定義したICommandに、ボタンではないコントロールをクリックしたときの処理を書きたい。
(ViewModelで定義したICommandに→表現正しいのか?)

EventTriggerを使う

EventTriggerを使う前に、下記の手順が必要。

xaml側

a.xaml
<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="1">このグリッドを押すと、トリガーが発火します2
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseDown">
                    <!-- ViewModelのメソッドの呼び方1 -->
                    <i:InvokeCommandAction Command="{Binding func}"/>
                    <!-- ViewModelのメソッドの呼び方2 -->
                    <ei:CallMethodAction TargetObject="{Binding}" MethodName="EventFunc"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBlock>
    </Grid>
</Window>

ビューモデル側

using System.Windows;
using System.Windows.Input;

namespace WpfApp1
{
    class ViewModel
    {
        public ICommand func { get; set; }

        public ViewModel()
        {
            func = new DelegateCommand(
                () =>
                {
                    MessageBox.Show("トリガー発火");
                    return;
                });
        }

        public void EventFunc()
        {
            MessageBox.Show("CallMethodAction でトリガー発火");
        }
    }
}

その他の部分のコード

こちらに置いておく。
https://github.com/tera1707/WPF-/tree/master/003_EventTrigger

memo

  • トリガーには、EentTrigger以外に、
    • TimerTrigger
    • PropertyChangedEventTrigger
  • トリガー発火時に呼ばれるActionには、下記のようなものがある。
    • CallMethodAction
    • ChangePropertyAction
    • ControlStoryboardAction
    • GoToStateAction
    • InvokeCommandAction

かずきのBlogより。
上のサンプルでは、EventTriggerで、CallMethodAction とInvokeCommandAction を使っている。
この辺りをうまく使えば、xamlだけでいろいろなことができそう。(未検証)

参考

WPF4.5入門 その58「Behavior」
https://blog.okazuki.jp/entry/2014/12/21/205558

【WPF】【MVVM】イベントをViewModelのメソッドに割り当てる。2
http://pro.art55.jp/?eid=1303821

GUIのマウス/キー操作処理をコードビハインドから駆逐する
https://qiita.com/hotelmoskva_/items/13ecc724bdad00078c16

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13