如何使用转换器在WPF中设置SystemColor.HighlightBrushKey

如何使用转换器在WPF中设置SystemColor.HighlightBrushKey,wpf,background,selection,converter,Wpf,Background,Selection,Converter,我正在尝试设置SystemColor.HighlightBrushKey始终比所选行的背景暗一点。 因此,我使用以下代码: App.xaml: 现在的问题是转换器只被调用一次。我的意思是,如果我启动程序并点击转换器被调用的任何一行。如果我单击另一行,则不会调用DataGrid或Control转换器 您知道如何解决此问题吗?问题在于此绑定: Color="{Binding Background, Converter={StaticResource SelectionBackgroundConv

我正在尝试设置SystemColor.HighlightBrushKey始终比所选行的背景暗一点。 因此,我使用以下代码:

App.xaml:

现在的问题是转换器只被调用一次。我的意思是,如果我启动程序并点击转换器被调用的任何一行。如果我单击另一行,则不会调用DataGrid或Control转换器


您知道如何解决此问题吗?

问题在于此绑定:

Color="{Binding Background, Converter={StaticResource SelectionBackgroundConverter}}"
没有
,当前上下文中不存在
后台
属性。将其更改为:

Color="{Binding Source={x:Static SystemColors.HighlightBrush}, Converter={StaticResource SelectionBackgroundConverter}}"
您的转换器将被调用。虽然你的转换器中有bug,但这应该可以让你开始。还应考虑:

  • 冻结刷子
  • 缓存笔刷(如果你在应用程序中经常这样做)
using System; 
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace WPFTests2
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        LB.Items.Add("Text1");
        LB.Items.Add("Text2");
        LB.Items.Add("Text3");
        LB.Items.Add("Text4");
        LB.Items.Add("Text5");
    }
}

public class SelectionBackgroundConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        if (value != null)
        {
            SolidColorBrush brush = (SolidColorBrush)value;
            Color newCol = brush.Color;
            newCol.R -= 10;
            newCol.G -= 10;
            newCol.B -= 10;
            BrushConverter conv = new BrushConverter();
            Brush newBrush = (Brush)conv.ConvertTo(newCol, typeof(Brush));
            return newBrush;
        }
        return Brushes.Transparent;
    }

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        //never called
        return null;
    }
}
}
public class SelectionBackgroundConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        if (value != null)
        {
            SolidColorBrush brush = (SolidColorBrush)value;
            Color newCol = brush.Color;
            newCol.R -= 10;
            newCol.G -= 10;
            newCol.B -= 10;
            return new SolidColorBrush(newCol);
        }
        return Brushes.Transparent;
    }

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        // we don't intend this to ever be called
        return null;
    }
Color="{Binding Background, Converter={StaticResource SelectionBackgroundConverter}}"
Color="{Binding Source={x:Static SystemColors.HighlightBrush}, Converter={StaticResource SelectionBackgroundConverter}}"