我有一个数据网格,行的颜色不同,具体取决于其中记录的状态(有效为白色,问题为金色,禁止为红色)。
The problem is when the row is selected they all turn a uniform color and it becomes impossible to determine the status anymore. 我想以与此类似的方式绑定突出显示颜色:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding Member, Converter={StaticResource MemberBackgroundConverter}}" />
<Setter Property="HighlightBrushKey" Value="{Binding Member, Converter={StaticResource MemberHighlightConverter}}" />
</Style>
</DataGrid.RowStyle>
上面的第一个 Setter 有效。有没有办法让第二个工作?有什么方法可以设置每行的 HighlightBrush 吗?
编辑:以下是我目前正在工作的内容。我并不是说这是最好的方法,只是这种方法有效。
XAML:
<DataGrid.Resources>
<SolidColorBrush x:Key="SelectionBackgroundColorKey" />
<LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="{Binding Source={StaticResource SelectionBackgroundColorKey}, Path=Color}" Offset="0.0" />
<GradientStop Color="White" Offset="0.3" />
<GradientStop Color="{Binding Source={StaticResource SelectionBackgroundColorKey}, Path=Color}" Offset="1.0" />
</LinearGradientBrush>
<SolidColorBrush x:Key="SelectionTextColorKey" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{Binding Source={StaticResource SelectionTextColorKey}, Path=Color}" />
</DataGrid.Resources>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="{Binding BindsDirectlyToSource=True, Converter={StaticResource ReservationBackgroundConverter}}" />
<Setter Property="Foreground" Value="{Binding IsNew, Converter={StaticResource IsNewForegroundConverter}}" />
</Style>
</DataGrid.RowStyle>
代码:
private void DataGridReservationsSelectionChanged(object argSender, SelectionChangedEventArgs argEvtArgs)
{
Reservation LocalReservation;
((SolidColorBrush) dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.SlateGray;
((SolidColorBrush) dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black;
LocalReservation = dataGridReservations.SelectedItem as Reservation;
if (LocalReservation == null)
{
return;
}
if(LocalReservation.IsArrived)
{
((SolidColorBrush)dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.ForestGreen;
((SolidColorBrush)dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black;
return;
}
//Is this Reservation a Problem?
if (LocalReservation.Member.IsProblem)
{
((SolidColorBrush) dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.Goldenrod;
((SolidColorBrush) dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black;
return;
}
//Is this Reservation Banned?
if (LocalReservation.Member.IsBanned)
{
((SolidColorBrush) dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.Firebrick;
((SolidColorBrush) dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black;
return;
}
}
这种方法允许我为每个独立行设置未选择的颜色,并为每个独立行设置选定的颜色。