<ListBox Name="listBox" SelectionMode="Extended">

<ListBox.Resources>

<Style TargetType="{x:Type ListBoxItem}">

<EventSetter Event="ListBoxItem.PreviewMouseLeftButtonDown" Handler="lbItem_PreviewMouseLeftButtonDown"/>

<EventSetter Event="ListBoxItem.PreviewMouseUp" Handler="lbItem_PreviewMouseUp"/>

<EventSetter Event="ListBoxItem.PreviewMouseMove" Handler="lbItem_PreviewMouseMove"/>

</Style>

</ListBox.Resources>

<x:Type TypeName="DependencyObject"/>

<x:Type TypeName="Visual"/>

<x:Type TypeName="UIElement"/>

<x:Type TypeName="FrameworkElement"/>

<x:Type TypeName="Control"/>

</ListBox>

private bool inMouseSelectionMode = false;

private List<ListBoxItem> selectedItems = new List<ListBoxItem>();

private void lbItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)

{

// MouseDown时清空已选Item

// 同时开始"inMouseSelectionMode"

foreach (var item in selectedItems)

{

item.ClearValue(ListBoxItem.BackgroundProperty);

item.ClearValue(TextElement.ForegroundProperty);

}

selectedItems.Clear();

inMouseSelectionMode = true;

}

private void lbItem_PreviewMouseUp(object sender, MouseButtonEventArgs e)

{

// MouseUp时停止"inMouseSelectionMode"

ListBoxItem mouseUpItem = sender as ListBoxItem;

inMouseSelectionMode = false;

}

private void lbItem_PreviewMouseMove(object sender, MouseEventArgs e)

{

ListBoxItem mouseOverItem = sender as ListBoxItem;

if (mouseOverItem != null && inMouseSelectionMode && e.LeftButton == MouseButtonState.Pressed)

{

// Mouse所在的Item设置高亮

mouseOverItem.Background = SystemColors.HighlightBrush;

mouseOverItem.SetValue(TextElement.ForegroundProperty, SystemColors.HighlightTextBrush);

if (!selectedItems.Contains(mouseOverItem)) { selectedItems.Add(mouseOverItem); }

}

}

  本文来自ny_dsc的博客,原文地址:http://hi.baidu.com/ny_dsc/blog/item/2bc69a31fdfb3186a8018e9a.html

[WPF][ListBox]鼠标拖拽多选,(Shift Key、Ctrl Key多选有效)(转)的更多相关文章

  1. 【WPF】鼠标拖拽功能DragOver和Drop

    在Winform里面实现拖入功能只要设置控件AllowDrop=true; 然后实现方法 //拖入 private void txtInputPath_DragOver(object sender, ...

  2. WPF 在image控件用鼠标拖拽出矩形

    原文:WPF 在image控件用鼠标拖拽出矩形 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee ...

  3. 设置zedgraph鼠标拖拽和局部放大属性(转帖)

    说一下几个属性的意义和具体应用: (1)鼠标拖拽显示区域 PanModifierKeys ->> Gets or sets a value that determines which mo ...

  4. Java3D读取3DMax模型并实现鼠标拖拽、旋转、滚轮缩放等功能

    /**-------------------------------------------------代码区--------------------------------------------- ...

  5. 2018-11-19-WPF-在image控件用鼠标拖拽出矩形

    title author date CreateTime categories WPF 在image控件用鼠标拖拽出矩形 lindexi 2018-11-19 15:35:13 +0800 2018- ...

  6. CSharpGL(20)用unProject和Project实现鼠标拖拽图元

    CSharpGL(20)用unProject和Project实现鼠标拖拽图元 效果图 例如,你可以把Big Dipper这个模型拽成下面这个样子. 配合旋转,还可以继续拖拽成这样. 当然,能拖拽的不只 ...

  7. JavaScript鼠标拖拽特效及相关问题总结

    #div1{width:200px;height:200px;background:red;position:absolute;} #div2{width:200px;height:200px;bac ...

  8. 一款基于jQuery的支持鼠标拖拽滑动焦点图

    记得之前我们分享过一款jQuery全屏广告图片焦点图,图片切换效果还不错.今天我们要分享另外一款jQuery焦点图插件,它的特点是支持鼠标拖拽滑动,所以在移动设备上使用更加方便,你只要用手指滑动屏幕即 ...

  9. 【狼】unity 鼠标拖拽物体实现任意角度自旋转

    主要涉及函数 Input.GetAxis(“Mouse x”) 可取得鼠标横向(x轴)移动增量 Input.GetAxis(“Mouse y”) 可取得鼠标竖向(y轴)移动增量 通过勾股定理获取拖拽长 ...

随机推荐

  1. ARC097D Equals

    传送门 题目 We have a permutation of the integers from 1 through N, p1, p2, .., pN. We also have M pairs ...

  2. TMF SID中的角色模式

    角色模式 Copyright © TeleManagement Forum 2013. All Rights Reserved. This document and translations of i ...

  3. 【Strtus2】

    基于mvc设计模式的web应用框架!strtus2作为控制器来建立模型与视图数据的交互.

  4. 20170527关于Thingking in Java

    由于工作上的需求,要开始学习Android开发,所以先开始看一些Java方面的知识.学习从Thingking in Java开始,看了一下第一张,感觉真的是一本好书,希望自己努力把他看完,加油! 第一 ...

  5. Go:定时执行任务time.sleep和time.tick的优劣

    golang 写循环执行的定时任务,常见的有以下三种实现方式:1.time.Sleep方法: for { time.Sleep(time.Second) fmt.Println("我在定时执 ...

  6. cogs 2123. [HZOI 2015] Glass Beads

    2123. [HZOI 2015] Glass Beads ★★★   输入文件:MinRepresentations.in   输出文件:MinRepresentations.out   简单对比时 ...

  7. ajax跨域问题解决方案(jsonp,cors)

    跨域 跨域有三个条件,满足任何一个条件就是跨域 1:服务器端口不一致 2:协议不一致 3:域名不一致 解决方案: 1.jsonp 在远程服务器上设法动态的把数据装进js格式的文本代码段中,供客户端调用 ...

  8. iOS sqlite

    iOS sqlite数据库操作.步骤是: 先加入sqlite开发库libsqlite3.dylib, 新建或打开数据库, 创建数据表, 插入数据, 查询数据并打印 1.新建项目sqliteDemo,添 ...

  9. 将vue和element-ui写在一个html里面方便调试(小白篇)

    声明:纯属小白进门文档 vue的官方文档: https://vuejs.bootcss.com/v2/guide/ 第一步:引入vue.js <script src="https:// ...

  10. LDAP--对某些AD属性值是字节数组byte[]情况的类型转换方法

    //BitConverter.ToBoolean((searchResult.Properties["mDBUseDefaults"][0] as byte[]), 0); row ...