【问题标题】:Click on row in DataGrid does not select it单击 DataGrid 中的行不会选择它
【发布时间】:2017-02-10 09:08:48
【问题描述】:

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

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

<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>

【问题讨论】:

    标签: wpf wpfdatagrid


    【解决方案1】:

    您无法设置任何属性来更改此行为。如果没有单击单元格,则不应单击行。这就是控件的工作原理。

    但您可以轻松解决这个问题。只需处理DataGridRowMouseLeftButtonDown 事件并明确选择它:

    <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;
    }
    

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

    【讨论】:

      【解决方案2】:

      我认为这与DataGridCell的模板有关。我建议使用 DataGridTemplateColumn,其中未设置单元格的边距:

      <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>
                  <DataGridTemplateColumn Width="100">
                      <DataGridTemplateColumn.CellTemplate>
                          <DataTemplate>
                              <TextBlock Margin="15" Text="{Binding}" Background="Black" Foreground="White" />
                          </DataTemplate>
                      </DataGridTemplateColumn.CellTemplate>
                  </DataGridTemplateColumn>
              </DataGrid.Columns>
      

      【讨论】:

      • 是的,它有效,但对我来说似乎是一种解决方法。该行处理鼠标单击,但 IsSelected 未更新。我认为可能有一个特殊的属性(如 SelectionUnit)可以修改行为。
      猜你喜欢
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 2011-04-19
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      相关资源
      最近更新 更多