wpf - 单击 DataGrid 中的行未选中它

标签 wpf wpfdatagrid

enter image description here

黑色背景-单元格。
灰色背景-行。
蓝色背景 - 选定的行。

如果我点击行,它不会被选中。但是,如果我单击单元格,则会正确选择行。

<Window x:Class="Test021000.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid Width="200" Height="200" ItemsSource="{Binding}" AutoGenerateColumns="False" SelectionUnit="FullRow">
            <DataGrid.DataContext>
                <x:Array Type="{x:Type sys:String}">
                    <sys:String>1</sys:String>
                    <sys:String>2</sys:String>
                    <sys:String>3</sys:String>
                    <sys:String>4</sys:String>
                    <sys:String>5</sys:String>
                </x:Array>
            </DataGrid.DataContext>
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding}" Width="100">
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="{x:Type DataGridCell}">
                            <Setter Property="Background" Value="Black" />
                            <Setter Property="Foreground" Value="White" />
                            <Setter Property="Margin" Value="15" />
                        </Style>
                    </DataGridTextColumn.CellStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="Background" Value="LightGray" />
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Blue" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
    </Grid>
</Window>

最佳答案

您无法设置任何属性来更改此行为。基本上不应该在没有单击单元格的情况下单击一行。这就是控件的工作方式。

但是你可以很容易地解决这个问题。只需处理 MouseLeftButtonDown DataGridRow事件并明确选择它:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <EventSetter Event="MouseLeftButtonDown" Handler="DataGrid_MouseLeftButtonDown" />
        <Setter Property="Background" Value="LightGray" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Blue" />
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>
private void DataGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DataGridRow dgr = sender as DataGridRow;
    dgr.IsSelected = true;
}

当一个控件的行为与您喜欢的方式或您期望的方式不同时,您可以使用另一个控件,从头开始编写自己的控件,或者通过编写一些代码来修改现有控件的行为:)

关于wpf - 单击 DataGrid 中的行未选中它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42155441/

相关文章:

c# - 使用 DependencyProperty 的可见性绑定(bind)

WPF 工具包中的 WPF DataGrid 与 ListView - 有什么好处?

c# - 如何在 Window 启动 C# 时将 wpf 应用程序移动到最小化托盘?

c# - 使用页面和 BitmapImage 的 WPF 应用程序中的内存泄漏

c# - 在运行时转换值

c# - WPF MVVM Datagrid 单元格验证错误行为问题 : Why is my value being removed?

c# - WPF MVVM 检索数据网格选定的行

c# - 通过 DataGrid 在 DataTable 中插入新行时更新主键

c# - 增强 WPF GridView 中特定列的显示

c# - 在 C# 中确定正版 Windows 安装