简体   繁体   English

c#wpf需要使用鼠标多选网格单元格(非DataGrid控件)

[英]c# wpf need to multiselect grid cells with mouse (NOT DataGrid control)

I have a standard Grid in my WPF form with "n" rows and "n" columns. 我在WPF表单中有一个标准网格,其中包含“ n”行和“ n”列。

I wish to select and highlight the background of a selection of cells in one row by using the mouse left down and "dragging" the "highlight" across to the cells - each cell's background is highlighted for each one that the mouse-left-down moves over. 我希望通过使用鼠标左键向下拖动并将“突出显示”拖动到单元格中,以选择并突出显示一行中的单元格的背景-每个单元格的背景都会高亮显示,鼠标左键向下移动。

I currently have code working that will select one cell - using the mouse left down event that I have attached to the textblock assigned to the cell (row/column). 我目前使用的代码可以选择一个单元格-使用鼠标左击事件,该事件已附加到分配给该单元格(行/列)的文本块上。

Does anyone have any idea how I can do the multiple select? 有谁知道我如何进行多项选择?

I have seen this working on the telerik grid but I am not in the position to use the Telerik controls. 我已经看到在Telerik网格上可以正常工作,但是我无法使用Telerik控件。

(Googling brings up loads of Datagrid answers that do not answer the problem) (谷歌搜索会带来大量无法解决问题的Datagrid答案)

Will post code if I need to. 如果需要,将发布代码。

This is for the standard grid control not the datagrid control. 这是针对标准网格控件而非数据网格控件的。

Many thanks. 非常感谢。

You said that you have code to get your single cell selection working, so I assume that you have code that will determine whether there is a Grid at the MousePosition when the user clicks the mouse button. 您说您有使单个单元格选择正常工作的代码,所以我假设您有代码,当用户单击鼠标按钮时,该代码将确定MousePosition是否存在Grid

All you need to do to enable the multi select functionality is to also monitor the PreviewMouseLeftButtonUp event as well. 启用多选功能所需要做的就是也监视PreviewMouseLeftButtonUp事件。 If you detect that the mouse down point was over Grid cell (0, 1) and the mouse up point was over Grid cell (3, 1), then you can determine that the user has clicked and dragged across cells 1 - 4 in row 1 of the Grid . 如果您检测到鼠标向下的点位于Grid单元格(0,1)上并且鼠标向上的点位于Grid单元格(3,1)上,则可以确定用户在行中的单元格1-4上单击并拖动Grid 1。

thanks for your post sheridan - it helped me through the thought process of what I needed: 感谢您发布sheridan的内容-它帮助我完成了所需的思考过程:

capture the events for: 捕获以下事件:

  1. mouse left button down 鼠标左键按下

  2. mouse move 鼠标移动

  3. mouse left button up 鼠标左键向上

all sorted :) 全部排序:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM