Wpf中双击事件

需求:项目中需要为listbox的每一项添加一个双击事件

实现1:

<ListBox.ItemContainerStyle>
   <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
     <EventSetterEvent="MouseDoubleClick" Handler="RecoderItemDoubleClick"/>
  </ListBox.ItemContainerStyle>

 

RecoderItemDoubleClick 即为双击事件

缺点无法绑定实现(反正我现在还不知道咋绑)

 

实现2:

将listbox改成listview,利用其MouseDoubleClick实现

<ListView x:Name="lv"  SelectionMode="Single"

 ItemTemplate="{StaticResource RecodeTemplate}" ItemsSource="{Binding RecoderEntities}" >

 <i:Interaction.Triggers>

   <i:EventTrigger EventName="MouseDoubleClick">

    <command:EventToCommand Command="{Binding RecodeItemDoubleClick}"

             CommandParameter="{Binding ElementName=lv,Path=SelectedItem}" />

   </i:EventTrigger>

 </i:Interaction.Triggers>

 </ListView>

 

缺点:这是利用的listview的MouseDoubleClick,弊端不言而喻

 

实现3

利用MouseLeftButtonDown中的e.ClickCount获取点击次数

<DataTemplate x:Key="RecodeTemplate">

    <StackPanel  Orientation="Horizontal" Height="70" >

     <i:Interaction.Triggers>

        <i:EventTrigger EventName= "MouseLeftButtonDown">

             <command:EventToCommand Command="viewModel:MainMv.TestCmd"

                                    PassEventArgsToCommand="True" />

             </i:EventTrigger>

     </i:Interaction.Triggers>

     <TextBlock HorizontalAlignment="Stretch" 

Text="{Binding Index}" VerticalAlignment="Center" MinWidth="10" />

      <Image  Source="{Binding Image}"

HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" Width="Auto" MinWidth="95"/>

      <TextBlock HorizontalAlignment="Right"

Text="{Binding FunctionName}" VerticalAlignment="Center" MinWidth="65" Margin="2,0,0,0" />

    </StackPanel>

</DataTemplate>

 

 

TestCmd为静态属性

private static ICommand testCmd;

public static ICommand TestCmd

 {

    get

     {

        testCmd = testCmd ?? new RelayCommand<MouseButtonEventArgs>(e =>

                                                                      {

                                                         if (e.ClickCount == 2)

                                                                          {}

                                                                      });

                return testCmd;

            }

}

 

posted @ 2015-03-16 10:19  [在河之洲]  阅读(1688)  评论(0)    收藏  举报