[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
添付ビヘイビア自体については他のサイトにお任せします。
MVVMパターンを使用したときに、イベントとVMのコマンドをBindingしたいときに使えるみたいです。
以下のコードはWPF Model-View-ViewModel Toolkitを使用しています。
まずはXAMLです。
<!--前略-->
<Grid>
<ListView Name="listView" ItemsSource="{Binding Path=Table}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Name" />
<GridViewColumn DisplayMemberBinding="{Binding Path=Age}" Header="Age" />
</GridView>
</ListView.View>
</ListView>
</Grid>
<!--後略-->
いろいろは省略しますが、このGridViewの選択行が変わったときにメッセージボックスを出してみようと思います。
ついでにNameの値を拾ってみたりも。
まずはコマンドを用意します。
VMに以下のコードを追加します。
//前略
private DelegateCommand<string> changeNameCommand;public ICommand ChangeNameCommand
{
get
{
if (changeNameCommand == null)
{
changeNameCommand = new DelegateCommand<string>(ChangeName);
}
return changeNameCommand;
}
}private void ChangeName(string s)
{
MessageBox.Show(s);
}
//後略
普通にコマンドを作成します。
次に添付ビヘイビアを作成します。
//前略
namespace WpfModelViewTest.Behavior
{
class ListViewBehavior
{
#region SelectionChangedCommandpublic static readonly DependencyProperty SelectionChangedCommandProperty =
DependencyProperty.RegisterAttached(
"SelectionChangedCommand",
typeof(ICommand),
typeof(ListViewBehavior),
new UIPropertyMetadata(null, OnSelectionChangedCommandPropertyChanged));public static ICommand GetSelectionChangedCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(SelectionChangedCommandProperty);
}public static void SetSelectionChangedCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(SelectionChangedCommandProperty, value);
}
ここまではほぼ定型文です。
static void OnSelectionChangedCommandPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
ListView listView = obj as ListView;
if (listView == null)
{
return;
}ICommand command = (ICommand)e.NewValue;
if (command != null)
{
listView.SelectionChanged += new SelectionChangedEventHandler(OnSelectionChanged);
}
else
{
listView.SelectionChanged -= new SelectionChangedEventHandler(OnSelectionChanged);
}
}
ここで、SelectionChangedイベントが発生したときに関数を実行するようにします。
ListViewのイベントであれば、SelectionChanged以外も使用できます。
static void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListView listView = sender as ListView;
ICommand command = ListViewBehavior.GetSelectionChangedCommand(listView);
if (command != null)
{
var rowView = listView.SelectedItem as DataRowView;
var row = rowView.Row as NameAgeDataSet.DataTableRow;
if (row == null)
{
command.Execute("拾えませんでした");
}
else
{
command.Execute(row.Name);
}
}
}
#endregion
}
}
で、イベントが発生したときにコマンドとNameの値を取得して、コマンドを実行します。
最後に、XAMLを修正します。
<Window x:Class="WpfModelViewTest.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:WpfModelViewTest.Commands"
xmlns:l="clr-namespace:WpfModelViewTest.Behavior"
Title="Main Window" Height="400" Width="800">
重要なのはxmlns:lの行です。
添付ビヘイビアを作成したNamespaceを指定します。
<!--中略-->
<Grid>
<ListView l:ListViewBehavior.SelectionChangedCommand="{Binding Path=ChangeNameCommand}" Name="listView" ItemsSource="{Binding Path=Table}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Name" />
<GridViewColumn DisplayMemberBinding="{Binding Path=Age}" Header="Age" />
</GridView>
</ListView.View>
</ListView>
</Grid>
<!--後略-->
で、添付ビヘイビアにVMのコマンドをBindingします。
以上です。
あまり良くない方法だと思いますが、複数のイベントとコマンドをBindingしてみたサンプルをおいておきます。
http://blog.cnobi.jp/v1/blog/user/2eef8264e2b1da90a110941b349819ff/1274362947