2012-03-15 5 views
1

AttachedCommandBehavior V2を使用して、ダブルクリックなどのListBoxItemイベントをビューモデルに対して実行されるコマンドに変換しようとしています。スタイルを使用して複数のAttachedCommandBehaviorsを適用する

私はこれは私がエミュレートしようとしているサンプルコードで、複数のイベントのためのコマンドを起動します:

<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" x:Name="test"> 
    <local:CommandBehaviorCollection.Behaviors> 
      <local:BehaviorBinding Event="MouseLeftButtonDown" Action="{Binding DoSomething}" CommandParameter="An Action on MouseLeftButtonDown"/> 
      <local:BehaviorBinding Event="MouseRightButtonDown" Command="{Binding SomeCommand}" CommandParameter="A Command on MouseRightButtonDown"/> 
    </local:CommandBehaviorCollection.Behaviors> 
    <TextBlock Text="MouseDown on this border to execute the command"/> 
</Border> 

私はListBoxItemにそれを適用したいので、私はスタイルを通してそれをやろうとしています実行して:私はそれがCOMP、その後BehaviorBindingsの1をコメントアウト場合

<ListBox.ItemContainerStyle> 
    <Style> 
     <Setter Property="acb:CommandBehaviorCollection.Behaviors"> 
      <Setter.Value> 
       <acb:CommandBehaviorCollection> 
        <acb:BehaviorBinding Event="MouseDoubleClick" Command="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding}"/> 
        <acb:BehaviorBinding Event="KeyUp" Command="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding}"/> 
       </acb:CommandBehaviorCollection> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ListBox.ItemContainerStyle> 

をしかし、私はerror MC3089: The object 'CommandBehaviorCollection' already has a child and cannot add 'BehaviorBinding'. 'CommandBehaviorCollection' can accept only one child. Line 39 Position 11.

も言うことコードでコンパイルエラーを取得しますileが実行時のxamlロード例外 "値はnullにすることはできません。パラメータ名:プロパティ」ので、私は、私も正しいアプローチを取っているかはわからない

誰でもListBoxItem上に複数の動作バインディングを設定するには、正しい構文の例を提供することができます

+0

[関連する質問](http://stackoverflow.com/questions/1035023/firing-a-double-click-event-from-a-wpf-listview-item-using-mvvm)スタイルを使用して1つのCommandBehaviorのみを設定する例 – BrandonAGr

+0

見つかった記事に記載されているように2つの対話トリガを試しましたか?それが私が提案しようとしていたものです。 – Phil

答えて

1

私のソリューション。? DataItemは

0123のようなものである相互作用が引き金とItemTemplateにないItemContainerStyle。 これは、テキストボックス、全体ではなく、リストボックスの項目でコマンドをマウスのダブルクリックやキーを呼び出します。

<UserControl.Resources> 
    <DataTemplate DataType="{x:Type ViewModel:DataItem}" x:Key="ItemTemplate"> 
     <ContentControl> 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="MouseDoubleClick"> 
        <i:InvokeCommandAction Command="{Binding DoubleClickCommand}"/> 
       </i:EventTrigger> 
       <i:EventTrigger EventName="KeyUp"> 
        <i:InvokeCommandAction Command="{Binding KeyUpCommand}"/> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
      <TextBox Text="{Binding Name}"> 
      </TextBox> 
     </ContentControl> 
    </DataTemplate> 
</UserControl.Resources> 

<ListBox x:Name="listBox" ItemTemplate="{StaticResource ItemTemplate}" ItemsSource={Binding Items} /> 

を使用しています

であり、DataContextのビューモデルセットにはIList<DataItems> Items{get; private set}プロパティがあります。

関連する問題