<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. Entity Framework Code-First(9.8):DataAnnotations - Column Attribute

    DataAnnotations - Column Attribute: Column attribute can be applied to properties of a class. Defaul ...

  2. CSS学习系列4 -- 再说CSS中的浮动运用及clear:left/right实际用法

    在 CSS学习系列2 -- CSS中的清除浮动 中,我们详细说了CSS中清除浮动的方法及使用 后来我自己在项目开发一个需要使用浮动的网页时,进行了实际运用,加上后来看到一篇好文章.所以就在这里再次写篇 ...

  3. oracle环境变量

    1---此部分引自http://hi.baidu.com/jason_xux/item/1f44681d356927fa756a8480  感谢 ORA_NLS33 环境变量ora_nls33定义'l ...

  4. C#----接口与多继承

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 接口 { ...

  5. 对Json的一些理解

    标准json格式:{"name":"王大昭","url":"https://www.cnblogs.com/codezhao/&q ...

  6. vue -- 使用sass并引入公共sass文件

    sass可以提高我们的开发效率,怎么在vue的项目中使用sass并且可以设置一些公共的文件呢? 使用sass 1.安装sass的依赖包 npm install --save-dev sass-load ...

  7. 洛谷P3803 【模板】多项式乘法(FFT)

    P3803 [模板]多项式乘法(FFT) 题目背景 这是一道FFT模板题 题目描述 给定一个n次多项式F(x),和一个m次多项式G(x). 请求出F(x)和G(x)的卷积. 输入输出格式 输入格式: ...

  8. [Xcode 实际操作]五、使用表格-(3)设置UITableView单元格图标

    目录:[Swift]Xcode实际操作 本文将演示如何给表格行设置图标. 打开资源文件夹[Assets.xcassets], 在资源文件夹中导入两张图片:一张彩色,一张灰色,作为单元格的图标. [+] ...

  9. PAT天梯赛L2-025 分而治之

    题目链接:点击打开链接 分而治之,各个击破是兵家常用的策略之一.在战争中,我们希望首先攻下敌方的部分城市,使其剩余的城市变成孤立无援,然后再分头各个击破.为此参谋部提供了若干打击方案.本题就请你编写程 ...

  10. php模拟post提交数据

    $data = '{ "id": "17999030", "method": "sayHello", "jso ...