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

发布于 2024-07-25 16:09:12 字数 3284 浏览 2 评论 0原文

我试图将 SystemColors.HighlightBrushKey 设置为始终比所选行的背景暗一点。 因此我使用此代码:

App.xaml:

    <WPFTests2:SelectionBackgroundConverter x:Key="SelectionBackgroundConverter"/>

    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding Background, Converter={StaticResource SelectionBackgroundConverter}}"/>

</Application.Resources>

窗口1.xaml:

<Window x:Class="WPFTests2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
    <ListBox x:Name="LB" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>

Window1.xaml.cs:

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;
    }

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

知道如何解决这个问题吗?

I im trying to set the SystemColors.HighlightBrushKey always a bit darker than the Background of the selected Row.
Therefore im using this Code:

App.xaml:

    <WPFTests2:SelectionBackgroundConverter x:Key="SelectionBackgroundConverter"/>

    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding Background, Converter={StaticResource SelectionBackgroundConverter}}"/>

</Application.Resources>

Window1.xaml:

<Window x:Class="WPFTests2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
    <ListBox x:Name="LB" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>

Window1.xaml.cs:

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;
    }
}
}

The Problem is that the Converter never gets called...
Does anyone know how to set the Background of the selected Row a bit darker than it was before selecting it?

Any help is appreciated!

Update

It looks like its working but unfortunately not completely.
I've corrected the Converter to look like this:

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;
    }

Now the problem is that the Converter only gets called once. I mean if I start the Program and click on any Row the Converter gets called. If I then click into another Row, DataGrid or Control the Converter doesn't get called.

Any Idea how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殤城〤 2024-08-01 16:09:12

问题出在这个绑定中:

Color="{Binding Background, Converter={StaticResource SelectionBackgroundConverter}}"

没有 Source,并且 Background 属性在当前上下文中不存在。 将其更改为:

Color="{Binding Source={x:Static SystemColors.HighlightBrush}, Converter={StaticResource SelectionBackgroundConverter}}"

您的转换器将被调用。 虽然您的转换器中存在错误,但这应该可以帮助您入门。 另请考虑:

  • 冻结画笔
  • 缓存画笔(如果您在应用程序中经常这样做)

The problem is in this binding:

Color="{Binding Background, Converter={StaticResource SelectionBackgroundConverter}}"

There is no Source, and the Background property won't exist in the current context. Change it to this:

Color="{Binding Source={x:Static SystemColors.HighlightBrush}, Converter={StaticResource SelectionBackgroundConverter}}"

And your converter will be called. You have bugs in your converter though, but that should get you started. Also consider:

  • freezing the Brush
  • caching the Brush (if you do this a lot in your app)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文