<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. Hibernate存储date/datetime问题,解决java的date与mysql的datetime不兼容

    主要原因是出在Hibernate的配置文件中. 症状 1. java文件类型java.util.Date,数据库类型datetime,Hibernate配置文件用date,存储进数据库的时间只有年月日 ...

  2. Unity3D 脚本模板修改方法

    默认情况下,在Unity中创建C#脚本都会默认生成以下代码模板. using System.Collections; using System.Collections.Generic; using U ...

  3. Boost Python学习笔记(二)

    你将学到什么 如何在Python中调用C++代码 如何在C++中调用Python代码 在Python中调用C++代码 首先定义一个动物类(include/animal.h) #pragma once ...

  4. 【leetcode 3. 无重复字符的最长子串】解题报告

    思路:滑动窗口的思想 方法一:滑动窗口 int lengthOfLongestSubstring(string s) { /* 控制一个滑动窗口,窗口内的字符都是不重复的,通过set可以做到判断字符是 ...

  5. bzoj 3944: Sum(杜教筛)

    3944: Sum Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4930  Solved: 1313[Submit][Status][Discuss ...

  6. 2017-10-26 NOIP模拟赛2

    财富 (treasure) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有n个小伙伴.每个小伙伴有一个身高hi. 这个游戏是这样的,LYK生活的环境是 ...

  7. Mysql-5-数据表的基本操作

    1.创建表:之前需要use database database_name 然后create table 表名(): 例:创建员工表tb_employee1,结构如下表所示 字段名称 数据类型 备注 i ...

  8. Python Day24

    AJAX 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作 ...

  9. 响应式Web

    响应式布局的核心是:适配不同视口大小的流式布局. RWD和AWD RWD:Responsive Web Design AWD:Adaptive Web Design 实现RWD,多使用流式布局.针对所 ...

  10. Vue里的nextTick方法

    官方解释: 在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的 DOM. 自己总结: `Vue.nextTick(callback)`,当数据发生变化,更新后执 ...