2017-02-10 20 views
2

enter image description hereDataGridの行をクリックしても選択されません

黒の背景セル。 グレーの背景 - 行。 青の背景 - 選択した行。

私が行をクリックすると、選択されません。ただし、セルをクリックすると、行が正しく選択されます。

<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のテンプレートに関連していると思います。あなたはこの動作を変更するように設定することができますプロパティがない

<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

:私はマージンがセルの設定されていないDataGridTemplateColumnを、使用することをお勧めします。基本的にセルがクリックされることなく、行がクリックされることはありません。これがコントロールの仕組みです。

しかし、これを簡単に回避できます。ただ、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; 
} 

制御はあなたが好きなようまたはあなたが期待するように動作しない場合は、あなたが書いた、別のコントロールを使用しますかあなた自身のコードを書いたり、既存のコードの動作を変更したりすることができます:)

関連する問題