本文介绍了防止WPF DataGrid中的多行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF DataGrid中有以下XAML标记:

I have the following XAML markup in a WPF DataGrid:

<DataGrid ItemsSource="{Binding ResultList}" Grid.ColumnSpan="4" Grid.Row="7" Height="150"
              HorizontalAlignment="Left" Margin="10,0,0,0" Name="gvResults"
              VerticalAlignment="Bottom" Width="590" AutoGenerateColumns="False" SelectionChanged="gvResults_SelectionChanged"
               SelectionUnit="FullRow">
        <DataGrid.Columns>
            <DataGridTextColumn IsReadOnly="True" Binding="{Binding Name}" Header="Name" ScrollViewer.VerticalScrollBarVisibility="Auto" Width="190" />
            <DataGridTextColumn IsReadOnly="True" Binding="{Binding Surname}" Header="Surname" Width="190" />
            <DataGridTextColumn IsReadOnly="True" Binding="{Binding Age}" Header="Age" Width="*" />
        </DataGrid.Columns>
    </DataGrid>

是否可以阻止用户在按住Ctrl键或选择多行一个鼠标?

Is it possible to prevent users from selecting multiple rows while holding down the Ctrl key or selecting multiple rows with a mouse?

我在SelectionChanged事件中尝试过以下代码,但它不起作用:

I've tried the following code in the SelectionChanged event but it does not work:

        private void gvResults_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (gvResults.SelectedItems.Count > 1)
            {
                e.Handled = true;
            }
        }


推荐答案

尝试指定< DataGrid SelectionMode =Single
和可选 SelectionUnit =FullRow

SelectionMode的可用选项是

The available options for SelectionMode are


  • 单个

  • 扩展

和SelectionUnit是

and for SelectionUnit are


  • 单元格

  • FullRow

  • CellOrRowHeader

这篇关于防止WPF DataGrid中的多行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:49