3

ユーザーが ListView の項目をクリックしたときに、ViewModel でコマンドを実行しようとしています。ListViewItemXAML に a を追加すると、その に a を追加するだけで済みMouseBindingますInputBindings

<ListView>
<ListView.View>
   <GridView>
      <GridViewColumn Header="Test" />
   </GridView>
   </ListView.View>
   <ListViewItem Content="Item 1" >
      <ListViewItem.InputBindings>
         <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleClickCommand}" />
      </ListViewItem.InputBindings>
 </ListViewItem>
 </ListView>

しかし、データバインドされた ListView でこれを実現するにはどうすればよいでしょうか?

<ListView ItemsSource="{Binding Patients}">
<ListView.View>
    <GridView>
        <GridViewColumn Header="Test" />
    </GridView>
    <!-- How to set the MouseBinding for the generated ListViewItems?? -->
</ListView.View>

ListViewItemスタイルを定義ControlTempalteし、ListViewItem. ただし、より簡単な解決策があることを願っています。

敬具、マイケル

4

2 に答える 2

5

ControlTemplateスタイルを使用してonを置き換えることListViewItemは、悪い解決策ではありません。実際、それはおそらく私の最初の選択でしょう。

ListViewItem同じ種類を実現する別の方法は、スタイルでカスタムの添付プロパティを使用することです。

<Style TargetType="ListViewItem">
  <Setter Property="local:AddToInputBinding.Binding">
    <Setter.Value>
      <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleClickCommand}" />    
    </Setter.Value>
  </Setter>
  ...

これを行うには、 MyBindingHandler.AddBinding 添付プロパティを作成する必要があります。

public class AddToInputBinding
{
  public static InputBinding GetBinding(... // create using propa snippet
  public static void SetBinding(...
  public static readonly DependencyProperty BindingProperty = DependencyProperty.RegisterAttached(
    "Binding", typeof(InputBinding), typeof(AddToInputBinding), new PropertyMetadata
  {
    PropertyChangedCallback = (obj, e) =>
    {
      ((UIElement)obj).InputBindings.Add((InputBinding)e.NewValue);
    }
  }));
}

これを拡張して複数のバインドを処理することもできますが、おわかりのように: このクラスを使用すると、任意のスタイル内に InputBinding を追加できます。

DoubleClick バインディングは、テンプレート内の別のコントロールではなく ListBoxItem で直接定義されるため、このソリューションは、あなたが行っていることよりも望ましい場合があります。ただ、個人の好みによるところが大きいと思います。

于 2010-01-26T09:06:59.810 に答える
1

私は次のことを行うことでこれを回避することができました:

1)System.Windows.InteractivityDLLへの参照を追加しました(で見つかりましたC:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries

2)これをXAMLファイルに追加しました:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

3)これを私のListView内に追加しました:

<ListView ...>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDoubleClick">
            <i:InvokeCommandAction Command="{x:Static local:MainWindow.RoutedCommandEditSelectedRecordWindow}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    ...

</ListView>
于 2012-01-04T02:53:02.007 に答える