1

SystemColors.HighlightBrushKey を、選択した行の背景よりも常に少し暗く設定しようとしています。したがって、このコードを使用しています:

アプリ.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;
    }
}
}

問題は、コンバーターが呼び出されないことです...選択した行の背景を選択する前よりも少し暗く設定する方法を知っている人はいますか?

どんな助けでも大歓迎です!

アップデート

機能しているように見えますが、残念ながら完全ではありません。コンバーターを次のように修正しました。

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

問題は、Converter が 1 回しか呼び出されないことです。つまり、プログラムを開始して、コンバーターが呼び出される任意の行をクリックした場合です。その後、別の行、DataGrid、または Control をクリックしても、コンバーターは呼び出されません。

これを修正する方法はありますか?

4

1 に答える 1

2

問題はこのバインディングにあります:

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

はなくSourceBackgroundプロパティは現在のコンテキストに存在しません。これに変更します:

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

そして、コンバーターが呼び出されます。コンバーターにはバグがありますが、それで始められるはずです。また、考慮してください:

  • ブラシを凍結する
  • ブラシのキャッシュ(アプリでこれを頻繁に行う場合)
于 2009-06-29T14:02:46.800 に答える