已选择行时,鼠标拖动上的多重选择功能无法正常工作 [英] Multiselection on mouse drag is not working properly when a row is already selected

查看:123
本文介绍了已选择行时,鼠标拖动上的多重选择功能无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据网格中,当一行已经被选中,并且如果希望通过拖动来选择连续的行在下面的方案中不起作用。

In a datagrid, when a row is already selected and if you wish to select the consecutive rows by dragging it does not work in the below scenario.

推荐答案

问题是焦点。虽然选择实际上是行,但请记住,每个单元格都是可视对象,可以是 focus ed。 DataGrid 在后台处理程序,根据目前具有焦点的单元格更改选择;对于你在做什么,没有什么问题,除了当你想使用鼠标拖动多选择。因此,在单击第一个单元格之后,必须立即从点击的单元格中删除焦点,并且 DataGrid 的选择已更改。对我来说有用的是将焦点改变为 DataGrid 本身。

The problem is focus. While the selection is in fact the row, remember that each cell is a visual object and can be focused. DataGrid has handlers behind the scenes that change the selection based on which cell currently has focus; for what you're doing, there's nothing wrong with that except when you want to use mouse drag for multi select. So you have to remove the focus from the clicked cell immediately after the first cell is clicked and the DataGrid's selection has changed. What worked for me is changing the focus to the DataGrid itself.

在XAML中:

<DataGrid x:Name="DataGridTopics" Grid.Row="0" Grid.Column="1" AutoGenerateColumns="False" SelectionChanged="datagridTopic_SelectionChanged" IsReadOnly="True" VerticalAlignment="Top" MouseLeftButtonDown="DataGridTopics_MouseLeftButtonDown" CurrentCellChanged="DataGrid_CurrentCellChanged">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Topics" CanUserSort="False" CanUserResize="False" CanUserReorder="False" Width="*" Binding="{Binding Text}" />
   </DataGrid.Columns>
</DataGrid>

代码背后:

private void DataGrid_CurrentCellChanged(object sender, EventArgs e)
{
    var dg = (DataGrid)sender;
    dg.Focus();
}

这篇关于已选择行时,鼠标拖动上的多重选择功能无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆