防止在WPF DataGrid中选择多行 [英] Prevent multiple row selection in WPF DataGrid

查看:219
本文介绍了防止在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


  • Cell

  • FullRow

  • CellOrRowHeader

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

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