需求:ListBox中的Item是按钮图片,要求单击和双击时触发不同的事件。

XAML中需要引入System.Windows.Interactivity.dll

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

该ListBox的关键代码如下。

<ListBox ItemsSource="{Binding YourList}">
<ListBox.Template>
<!-- 流式布局 左对齐 -->
<ControlTemplate TargetType="ListBox">
<WrapPanel Width="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=DataContext.YourWidth}" Orientation="Horizontal" IsItemsHost="True"/>
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<!-- 单击事件,传参Button自身 -->
<Button Width="160" Height="120" Background="Transparent" BorderBrush="#E12080" BorderThickness="1"
Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=DataContext.YourClickCommand}"
CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}}" BorderBrush="#E12080" BorderThickness="1">
<!-- 双击事件,传参父节点的Button -->
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction
Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=DataContext.YourDoubleClickCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type Button}}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<!-- ListBoxItem的内容 --> </Grid>
</Button>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

【WPF】图片按钮的单击与双击事件的更多相关文章

  1. GridView/DataGrid行单击和双击事件实现代码_.Net教程

    功能: 单击选中行,双击打开详细页面 说明:单击事件(onclick)使用了 setTimeout 延迟,根据实际需要修改延迟时间 ;当双击时,通过全局变量 dbl_click 来取消单击事件的响应  ...

  2. 支持行单击、双击事件的GridView和DataList控件(译)

    支持行单击.双击事件的GridView和DataList控件(译)         让GridView 和 DataList 控件响应鼠标单击.双击事件.并且,使用 ClientScript.Regi ...

  3. 禁用CMFCRibbonApplicationButton的单击和双击事件

    为了禁用CMFCRibbonApplicationButton的单击和双击事件,我重载了CMFCRibbonApplicationButton如下: 1. MyRibbonApplicationBut ...

  4. jquery处理单击和双击事件

    今天做div点击时,需要用到同一div的单击和双击事件,出现问题如下 例子: Html <body> <div id="div_1">单击双击我</d ...

  5. Android 自定义View实现单击和双击事件

    自定义View, 1. 自定义一个Runnable线程TouchEventCountThread ,  用来统计500ms内的点击次数 2. 在MyView中的 onTouchEvent 中调用 上面 ...

  6. [Javasript] 同时实现单击和双击事件

    在同一个元素上同时绑定单击和双击事件: JavaScript <script type="text/javascript"> var timer = 0; var de ...

  7. JS - 解决鼠标单击、双击事件冲突问题(原生js实现)

    由于鼠标双击时每一次触发双击事件都会引起两次单击事件和一次单击事件,原生的js不提供专门的双击事件. 因为业务原因,双击和单机都绑定了不同的业务,在双击的时候又触发了单机,影响了页面的正常显示 出现问 ...

  8. [转] Ext Grid (ExtJs)上的单击以及双击事件

    例1: 1.双击 var cb = new Ext.grid.RowSelectionModel({ singleSelect:true //如果值是false,表明可以选择多行:否则只能选择一行 } ...

  9. unity3D 游戏物体同时绑定单击、双击事件

    前言 在unity中我们常用的获取鼠标点击的方法有 在3D场景中,一般用在Update方法中,每一帧调用 void Update(){ )){ Debug.log("鼠标左键点击" ...

随机推荐

  1. 对扫描的pdf文件生成目录

    很多pdf文件是直接扫描生成的,于是它的内容都是一张张的图片,当然就更没有目录索引了. 有的时候想找某些内容,只能一点点的移动滚动条,非常不方便. 那么有什么方法能生成目录呢? 方法一:使用福昕pdf ...

  2. 【Java并发核心四】Executor 与 ThreadPoolExecutor

    Executor 和 ThreadPoolExecutor 实现的是线程池,主要作用是支持高并发的访问处理. Executor 是一个接口,与线程池有关的大部分类都实现了此接口. ExecutorSe ...

  3. update date and keep time

    select TO_CHAR((date '2010-01-02' + (sysdate-trunc(sysdate))),'YYYY-MM-DD HH:mi:ss') from dual;

  4. java 同步 synchronized

    http://www.cnblogs.com/Qian123/p/5691705.html http://www.cnblogs.com/GnagWang/archive/2011/02/27/196 ...

  5. Linux虚拟机上安装redis

    1.检查安装依赖程序 yum install gcc-c++ yum install -y tcl yum install wget 2.获取安装文件 wget http://download.red ...

  6. SQL Server 限定删除一行

    with t as ( )* from testtest where aa='aa' order by bb ) delete from t

  7. 极客无极限 一行HTML5代码引发的创意大爆炸

    摘要:一行HTML5代码能做什么?国外开发者Jose Jesus Perez Aguinaga写了一行HTML5代码的文本编辑器.这件事在分享到Code Wall.Hacker News之后,引起了众 ...

  8. 一款易搭建,运行快的Git服务器:Gitea安装教程

    说明:Gitea是从Gogs发展而来,同样的拥有极易安装,运行快速的特点,而且更新比Gogs频繁很多,维护的人也多,个人认为Gitea还是更好一些的,这里就说下安装方法. 截图 简介 Gitea是一个 ...

  9. LOJ.6282.数列分块入门6(块状链表/分块)

    题目链接 1.分块(vector)+重构 //直接上vector(本机还是比较慢的...) 某块size较大时O(n)重构 //注意细节 #include <cmath> #include ...

  10. Unity枚举和字符串的相互转换

    直接上代码,见下图: public enum enumEx { A, B, C, D, } public class enumTest : MonoBehaviour { void Start () ...