<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. CodeForces 492D Vanya and Computer Game (思维题)

    D. Vanya and Computer Game time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  2. nginx配置同一域名下,共存2个nodejs项目

    项目背景: 1.官网需要改版,使用nodejs nuxt框架进行重构 2.官网改版没有全部完成.但需要上线首页 项目需求: 1.让首页内容显示为新项目 2.让老官网的内容可以被访问到(比如www.n. ...

  3. PHP开源系统学习之fluxbb_1

    最近一直忙于做项目,虽说做了点新东西.感觉自己进步不是很大,总体水平还是跟半年前差不多,想到的东西跟以前差不多,写出来的东西也跟以前差不多.只是现在做的东西多些,比以前敢做了. 近期准备利用点时间,读 ...

  4. [java基础]short s1 = 1; s1 = s1 + 1;有什么错?short s1 = 1; s1 += 1;有什么错?

    为什么写这篇文章是因为搜到的答案里并没有阐明s1 = s1 + 1为什么就要转换为int类型. 由一下实验可知: public class test { public static void main ...

  5. java 提取(解压)zip文件中特定后缀的文件并保存到指定目录

    内容简介 本文主要介绍使用ZipFile来提取zip压缩文件中特定后缀(如:png,jpg)的文件并保存到指定目录下. 导入包:import java.util.zip.ZipFile; 如需添加对r ...

  6. 前端页面唯一字符串生成(Js)UUID

    function uuid() { var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i < 3 ...

  7. Tomcat&Servlet

    Tomcat&Servlet 一.web开发相关的概念 1. 软件架构 1.1 C/S架构 C:Client客户端, S:Server服务器 比如:QQ.微信.大型网游 优点: 显示效果炫 安 ...

  8. easyui-dialog对话框练习

    <div id="dl1" class="easyui-dialog" title="窗口" style="width:40 ...

  9. centos6.5下安装python3.6、pip、ipython

    一.先更换系统源 为了下载顺畅一般都会更改为国内源. 1 cd /etc/yum.repos.d/ 2 wget http://mirrors.163.com/.help/CentOS6-Base-1 ...

  10. tp5模型事件回调函数中不能使用$this

    tp5模型事件回调函数中不能使用$this,使用会报错,涉及到数据库操作使用Db类,不能使用$this->save()之类的方式 如果回调函数中需要使用类内函数,需要将函数定义为static,通 ...