Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

DataTriggerでコマンドのバインディングをマークアップに移動させる

アプリ実行

起動

猫の[歩く]をクリック

鈴木の[歩く]をクリック


ソースコード

App.xaml
<Application x:Class="CommandDataTriggerSample.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
</Application>
MainWindow.xaml
<Window x:Class="CommandDataTriggerSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:CommandDataTriggerSample"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="DataTemplate1">
            <UniformGrid>
                <TextBlock Text="{Binding Path=Name}"/>
                <Button
                    x:Name="walkButton"
                    Content="歩く"
                    Width="75"
                    Command="{x:Static local:MainWindow.AnimalWalkCommnad}"
                    CommandParameter="{Binding Path=Name}"
                    />
            </UniformGrid>
            <DataTemplate.Triggers>
                <!-- 人間だったらボタンのふるまいを上書き -->
                <DataTrigger Value="human">
                    <DataTrigger.Binding>
                        <Binding Path="Type" />
                    </DataTrigger.Binding>
                    <Setter
                        TargetName="walkButton"
                        Property="Command"
                        Value="{x:Static local:MainWindow.HumanWalkCommnad}" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ListBox x:Name="creatures" ItemTemplate="{DynamicResource DataTemplate1}" />
    </Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;
using System.Windows.Input;

namespace CommandDataTriggerSample {
    public partial class MainWindow : Window {

        public static readonly RoutedCommand AnimalWalkCommnad 
            = new RoutedCommand("Aninal", typeof(MainWindow));

        public static readonly RoutedCommand HumanWalkCommnad
            = new RoutedCommand("Human", typeof(MainWindow));

        public MainWindow() {
            InitializeComponent();

            this.creatures.ItemsSource = new Creature[] {
                new Creature() { Type = "animal", Name = "猫" },
                new Creature() { Type = "human", Name = "鈴木" },
                new Creature() { Type = "animal", Name = "犬" },
                new Creature() { Type = "human", Name = "佐藤" },
            };

            this.CommandBindings.Add(
                new CommandBinding(
                    AnimalWalkCommnad,
                    (s, e) => MessageBox.Show((string) e.Parameter + "は4本足で歩きます。")
                )
            );

            this.CommandBindings.Add(
                new CommandBinding(
                    HumanWalkCommnad,
                    (s, e) => MessageBox.Show((string) e.Parameter + "は2本足で歩きます。")
                )
            );

        }
    }
}
Creature.cs
namespace CommandDataTriggerSample {
    internal class Creature {
        public string Type { get; set; }
        public string Name { get; set; }
    }
}