2009-06-29 16 views
1

SystemColors.HighlightBrushKeyを常に選択したRowの背景より少し暗く設定しようとしています。WPFでコンバータを使用してSystemColors.HighlightBrushKeyを設定する方法

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

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

ご協力いただきましてありがとうございます。

アップデートは

それは残念ながらありません完全にその作業のように見えます。 は、私はこのように見えるように変換を修正しました:

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

今の問題は、コンバータは、一度だけ呼び出されるということです。つまり、プログラムを起動して、コンバーターが呼び出された行をクリックすると意味します。次に、別のRow、DataGrid、またはControlをクリックすると、Converterは呼び出されません。

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

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

は何Sourceありません、とBackgroundプロパティは、現在のコンテキスト内に存在しません。

答えて

2

問題は、この結合です。これに変更してください:

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

あなたのコンバータが呼び出されます。しかし、あなたのコンバータにはバグがありますが、それはあなたを始めさせるはずです。また、考慮してください。ブラシ

  • ブラシをキャッシュを凍結

    (あなたは、このアプリの中にたくさん行った場合)
  • 関連する問題