在 WPF 中单击并拖动选择框
是否可以在WPF中实现鼠标单击并拖动选择框。是否应该通过简单地绘制一个矩形,计算其点的坐标并评估该框内其他对象的位置来完成?或者还有其他一些方法吗?
您能提供一些示例代码或链接吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否可以在WPF中实现鼠标单击并拖动选择框。是否应该通过简单地绘制一个矩形,计算其点的坐标并评估该框内其他对象的位置来完成?或者还有其他一些方法吗?
您能提供一些示例代码或链接吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
这是我过去用来绘制拖动选择框的简单技术的示例代码。
XAML:
C#:
我写了一篇关于此的文章:
https:// /www.codeproject.com/Articles/148503/Simple-Drag-Selection-in-WPF
Here is sample code for a simple technique that I have used in the past to draw a drag selection box.
XAML:
C#:
I wrote an article about this:
https://www.codeproject.com/Articles/148503/Simple-Drag-Selection-in-WPF
您可以通过添加 InkCanvas 并将其 EditingMode 设置为 Select 来轻松获得此功能。尽管它主要用于 Tablet PC 墨水收集和渲染,但将其用作基本设计器界面非常容易。
You can get this functionality pretty easily by adding an InkCanvas and set its EditingMode to Select. Although it's primarily intended for Tablet PC ink collection and rendering, it's very easy to use it as a basic designer surface.
该项目创建了一个自定义
MultiSelector
,它支持多种选择方法,包括矩形“套索”样式:Teofil Cobzaru 开发多选择器
此处复制的时间太长。 IIRC 设计的关键元素是创建一个自定义
ItemContainer
,它知道如何与其MultiSelector
父级交互。这类似于ListBoxItem
/ListBox
。这可能不是最简单的方法,但是如果您已经使用某种类型的
ItemsControl
来托管可能需要选择的项目,那么它可以很容易地适合该设计。This project created a custom
MultiSelector
which supports several selection methods including a rectangular "lasso" style:Developing a MultiSelector by Teofil Cobzaru
It is far too long to reproduce here. The key elements of the design, IIRC, were to create a custom
ItemContainer
which knows how to interact with itsMultiSelector
parent. This is analagous toListBoxItem
/ListBox
.This is probably not the simplest possible approach, however if you are already using some type of
ItemsControl
to host the items which may need to be selected, it could fit into that design pretty easily.鼠标按下逻辑:
MouseDown logic: