2010-12-05 10 views
2

私は、リスト内の項目にダブルクリックイベントを検出したいのGridViewを持って、私はそれを実行します。のGridViewのDoubleClick

<ListView> 
    <ListView.View > 
     <GridView > 
      <GridViewColumn Header="FileName"> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <Label Content="{Binding FileName}" MouseDoubleClick="Configuration_MouseDoubleClick"/> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
      <GridViewColumn DisplayMemberBinding="{Binding CreationDate}" Header="Date"/> 
     </GridView> 
    </ListView.View> 
</ListView> 

問題は、私だけに取り付けることにより、doubleclicksを検出することができるということですテンプレート内のコントロール

MouseDoubleClickイベントをすべてListViewItemに添付するにはどうすればよいですか? PRISMのための解決策はありますか?

答えて

12

あなたは...背後にあるこの

<ListView ...> 
    <ListView.ItemContainerStyle> 
     <Style TargetType="{x:Type ListViewItem}"> 
      <EventSetter Event="MouseDoubleClick" Handler="ListViewItem_MouseDoubleClick"/> 
     </Style> 
    </ListView.ItemContainerStyle> 
</ListView> 

コードのようなItemContainerStyleでのListViewItemにMouseDoubleClickイベントを追加することができます

void ListViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
{ 
    //...    
} 
+0

歓声Meleakを、これはうまく動作します! – thumbmunkeys

+0

@pivotnig:喜んで聞いてください:) –

8

あなたはMVVMをやっている場合は、分離コードとの間のギャップを埋めることができますあなたのビューモデルを通常通りに - 添付された動作を使用して:

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Input; 

public sealed class HandleDoubleClickBehavior 
{ 
    public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
     "Command", typeof (ICommand), typeof (HandleDoubleClickBehavior), new PropertyMetadata(default(ICommand), OnComandChanged)); 

    public static void SetCommand(DependencyObject element, ICommand value) 
    { 
     element.SetValue(CommandProperty, value); 
    } 

    public static ICommand GetCommand(DependencyObject element) 
    { 
     return (ICommand) element.GetValue(CommandProperty); 
    } 

    public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached(
     "CommandParameter", typeof (object), typeof (HandleDoubleClickBehavior), new PropertyMetadata(default(object))); 

    public static void SetCommandParameter(DependencyObject element, object value) 
    { 
     element.SetValue(CommandParameterProperty, value); 
    } 

    public static object GetCommandParameter(DependencyObject element) 
    { 
     return (object) element.GetValue(CommandParameterProperty); 
    } 

    private static void OnComandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var c = d as Control; 
     if (c == null) 
      throw new InvalidOperationException($"can only be attached to {nameof(Control)}"); 
     c.MouseDoubleClick -= OnDoubleClick; 
     if (GetCommand(c) != null) 
      c.MouseDoubleClick += OnDoubleClick; 
    } 

    private static void OnDoubleClick(object sender, MouseButtonEventArgs e) 
    { 
     var d = sender as DependencyObject; 
     if (d == null) 
      return; 
     var command = GetCommand(d); 
     if (command == null) 
      return; 
     var parameter = GetCommandParameter(d); 
     if (!command.CanExecute(parameter)) 
      return; 
     command.Execute(parameter); 
    } 
} 

これでツールボックスは、あなたがこのようなXAMLを(PersonViewModelは、文字列プロパティNameTitle、および文字列パラメータを期待しSayHiCommandという名前ICommandプロパティが含ま仮定して)書き込むことができます。

<ListView ItemsSource="{Binding Persons}" > 
    <ListView.ItemContainerStyle> 
     <Style TargetType="ListViewItem"> 
      <Setter Property="local:HandleDoubleClickBehavior.Command" Value="{Binding SayHiCommand}" /> 
      <Setter Property="local:HandleDoubleClickBehavior.CommandParameter" Value="{Binding Name}" /> 
     </Style> 
    </ListView.ItemContainerStyle> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" /> 
      <GridViewColumn Header="Title" DisplayMemberBinding="{Binding Title}" /> 
     </GridView> 
    </ListView.View> 
</ListView> 
+0

ちょうど私が必要なもの、ありがとう。 –

関連する問題