2017-02-10 36 views
2

enter image description here单击DataGrid中的行不选中它

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

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

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

回答

1

我认为这与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> 
+0

是的,它的工作,但看起来像我的解决办法。该行处理鼠标单击,但IsSelected未更新。我想可能是有一个特殊的属性(如SelectionUnit),修改行为。 –

1

没有属性,您可以设置来改变这种行为。基本上没有单元被点击的情况下,行不应被点击。这是控制的工作原理。

但你可以很容易地解决这个问题。刚处理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; 
} 

当控件不表现你喜欢你所期望的方式或途径,你要么使用其他的控制,写你自己从头开始或通过编写代码修改现有的行为:)