2017-02-10 29 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; 
} 

當控件不表現你喜歡你所期望的方式或途徑,你要麼使用其他的控制,寫你自己從頭開始或通過編寫代碼修改現有的行爲:)

相關問題