我正在尝试动态更改WPF DataGrid的SelectionBackground。我尝试绑定它,但是没有用。我终于找到了以下内容,但我仍然无法提供动态的“白色停止”。 我真正想要的是直接绑定到LinearGradientBrush,随着情况的变化,我可以变亮和变暗但我会决定如何将“白色停止”绑定到动态变化的东西。
<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>
CodeBehind片段:
private void DataGridReservationsSelectionChanged(object argSender, SelectionChangedEventArgs argEvtArgs)
{
Color localBackgroundColorKey = ApplicationSettings.ReservationNormalSelectedBackgroundColor;
Color localTextColorKey = ApplicationSettings.ReservationNormalSelectedTextColor;
Reservation localReservation = dataGridReservations.SelectedItem as Reservation;
if (localReservation == null)
{
return;
}
if (localReservation.IsArrived)
{
localBackgroundColorKey = ApplicationSettings.ReservationArrivedSelectedBackgroundColor;
localTextColorKey = ApplicationSettings.ReservationArrivedSelectedTextColor;
}
else if (localReservation.IsCanceled)
{
localBackgroundColorKey = ApplicationSettings.ReservationCanceledSelectedBackgroundColor;
localTextColorKey = ApplicationSettings.ReservationCanceledSelectedTextColor;
}
... etc...
((SolidColorBrush)dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = localBackgroundColorKey;
((SolidColorBrush)dataGridReservations.Resources["SelectionTextColorKey"]).Color = localTextColorKey;
}
答案 0 :(得分:1)
我想出了我想要的东西。 如果我向DataGrid资源添加更多的Key颜色,我可以在LinearGradientBrush中使用它们。
即
<SolidColorBrush x:Key="SelectionBackgroundColorKey" />
<SolidColorBrush x:Key="SelectionBackgroundWhiteStopKey" /> <!-- Added a WhiteStopKey to the resources -->
<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="{Binding Source={StaticResource SelectionBackgroundWhiteStopKey}, Path=Color}" Offset="0.3" /> <!-- Use the WhiteStopKey in the LinearGradientBrush -->
<GradientStop Color="{Binding Source={StaticResource SelectionBackgroundColorKey}, Path=Color}" Offset="1.0" />
</LinearGradientBrush>
...然后,在代码中......
((SolidColorBrush)dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = localBackgroundColorKey;
((SolidColorBrush)dataGridReservations.Resources["SelectionBackgroundWhiteStopKey"]).Color = localBackgroundColorKey.Lighten(1.7F);
注意:Color.Lighten()是我编写的扩展方法,而不是Framework方法。
用来解释:绝望是发明的母亲。