黑色背景 - 單元格。 灰色的背景 - 行。 藍色背景 - 選定的行。
如果我點擊行,它不會被選中。但是,如果我單擊單元格,行選擇正確。
<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>
是的,它的工作,但看起來像我的解決辦法。該行處理鼠標單擊,但IsSelected未更新。我想可能是有一個特殊的屬性(如SelectionUnit),修改行爲。 –