如何使用一个转换器设置SystemColors.HighlightBrushKey在WPF [英] How to set the SystemColors.HighlightBrushKey in WPF with a Converter
问题描述
我我尝试设置SystemColors.HighlightBrushKey总是比所选行的背景暗一点。
因此,即时通讯使用这种code:
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中:
App.xaml:
<WPFTests2:SelectionBackgroundConverter x:Key="SelectionBackgroundConverter"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding Background, Converter={StaticResource SelectionBackgroundConverter}}"/>
</Application.Resources>
Window1.xaml:
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:
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?
任何帮助AP preciated!
Any help is appreciated!
更新
它看起来像它的工作但遗憾的是没有完全。
我已经纠正了该转换器看起来像这样:
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;
}
现在的问题是,只有转换器被调用一次。我的意思是,如果我启动程序,然后单击任一行的转换被调用。如果我再点击进入另一行,DataGrid中或控制转换器不会被调用。
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?
推荐答案
现在的问题是在这个绑定:
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:
- 冻结刷
- 缓存刷(如果你这样做了很多在您的应用程序)
这篇关于如何使用一个转换器设置SystemColors.HighlightBrushKey在WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!