Architect's Log

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

コマンドとデータバインディングの統合

アプリ実行

起動

.jpg以外の[表示]をクリック

.jpgの[表示]をクリック


ソースコード

App.xaml
<Application x:Class="WpfApplication7.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="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:WpfApplication7"
        Title="MainWindow" Height="100" Width="150">
    <ListBox Margin="2" Name="files">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <WrapPanel>
                    <TextBlock Text="{Binding Path=Name}" />
                    <Button
                        CommandParameter="{Binding Path=FullName}">
                        <Button.Command>
                            <Binding>
                                <Binding.Converter>
                                    <l:FileToCommandConverter />
                                </Binding.Converter>
                            </Binding>
                        </Button.Command>
                        表示
                    </Button>
                </WrapPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Window>
MainWindow.xaml.cs
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication7 {
    public partial class MainWindow : Window {
        public static readonly RoutedCommand OpenCommand
            = new RoutedCommand("Open", typeof(MainWindow));

        public static readonly RoutedCommand BlockedCommand
            = new RoutedCommand("Blocked", typeof(MainWindow));

        public MainWindow() {
            InitializeComponent();

            FileInfo[] fileList = new DirectoryInfo(@"C:\temp").GetFiles("*.*");
            files.ItemsSource = fileList;

            CommandBindings.Add(
                new CommandBinding(OpenCommand,
                (s, e) => {
                    Process.Start("mspaint.exe", (string) e.Parameter);
                }));

            CommandBindings.Add(
                new CommandBinding(BlockedCommand,
                (s, e) => {
                    MessageBox.Show((string)e.Parameter, "ブロックされました");
                }));
        }
    }
}
FileToCommandConverter.cs
using System;
using System.Globalization;
using System.IO;
using System.Windows.Data;

namespace WpfApplication7 {
    class FileToCommandConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            string extension = ((FileInfo) value).Extension.ToLowerInvariant();

            if (extension == ".jpg") {
                return MainWindow.OpenCommand;
            }
            return MainWindow.BlockedCommand;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
    }
}