忍者ブログ
ゲーム関連の話題に絞っていましたが、全然書かないのでいろいろ解禁してみたり。
[16]  [15]  [14]  [13]  [12]  [11]  [10]  [9]  [8]  [7
×

[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 SelectionChangedCommand

  public 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

PR
この記事にコメントする
お名前
タイトル
文字色
メールアドレス
URL
コメント
パスワード   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
この記事へのトラックバック
この記事にトラックバックする:
Twitter
Twitter Update
ブログ内検索
カレンダー
05 2025/06 07
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
最新コメント
最新トラックバック
プロフィール
HN:
100poisha
性別:
非公開
アクセス解析
Copyright © 100poishaのブログ All Rights Reserved.
Designed by north sound
Powered by Ninja Blog

忍者ブログ [PR]