WPF Xaml 标记 TextToBrushConverter Dictionary<string,Brush> 可能吗?
WPF Xaml markup TextToBrushConverter Dictionary<string,Brush> possible?
我的目标是根据数据源的某个文本 属性 更改控件的背景(画笔)。
当我的转换器只有 2 个画笔属性时,我在一个更简单(有效)的示例中做到了这一点:
<local:ListErrorWarndescriptionToColorConverter x:Key="ErrorListToColor"
NormalBrush="Transparent" ErrorBrush="Red" WarnBrush="Yellow"/>
下一步是编写转换器来处理 2 个以上的字符串
我的转换器代码:
public class StringToBrushDictionary : Dictionary<string, Brush> { }
[ValueConversion(typeof(string), typeof(Brush))]
public sealed class TextToBrushConverter : IValueConverter
{
public StringToBrushDictionary LookUpTable { get; set; }
public Brush Default { get; set; }
public TextToBrushConverter()
{
// set defaults
LookUpTable = new StringToBrushDictionary();
Default = Brushes.Transparent;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var collection = value as string;
if (collection == null)
return Default;
if (LookUpTable.ContainsKey(collection))
return LookUpTable[collection];
return Default;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
我现在的问题是:如何在 xaml 中填写该词典
我找到了 this 8 year old question 但它并不是我所需要的,因为它使用字符串和 int,两个 "native" 数据类型并且还使用 VS 2010,它不支持字典(根据那里的答案)。那里的一个答案说后来的 XAML 版本可以做到这一点,但 VS 2010 还没有实现它。
这是我对转换器标记的看法以及我已经尝试过的内容:
<Application x:Class="BetterListbox.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BetterListbox"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:Specialized="clr-namespace:System.Collections.Specialized;assembly=System"
StartupUri="MainWindow.xaml">
<Application.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis" />
<local:ListErrorWarndescriptionToColorConverter x:Key="ErrorListToColor"
NormalBrush="Transparent" ErrorBrush="Red" WarnBrush="Yellow"/>
<local:TextToBrushConverter x:Key="WarnErrorToBrush" Default="Red">
<local:TextToBrushConverter.LookUpTable>
<local:StringToBrushDictionary>
<Brush x:Key="Error" >
...?
</Brush>
<collections:DictionaryEntry x:Key="d" Value="Brushes.Red" />
</local:StringToBrushDictionary>
</local:TextToBrushConverter.LookUpTable>
</local:TextToBrushConverter>
</Application.Resources>
</Application>
是否可以在 xaml 中填写 LookUpTable
,如果可以,如何填写?
你可以这样填写:
<my:TextToBrushConverter x:Key="textToBrush">
<my:TextToBrushConverter.LookUpTable>
<!-- LookUpTable["red"] = Brushes.Red -->
<x:Static MemberType="Brushes" Member="Red" x:Key="red" />
<!-- LookUpTable["aqua"] = new SolidColorBrush(Colors.Aqua) -->
<SolidColorBrush Color="Aqua" x:Key="aqua" />
<!-- custom color brush -->
<SolidColorBrush Color="#234433" x:Key="green" />
</my:TextToBrushConverter.LookUpTable>
</my:TextToBrushConverter>
我的目标是根据数据源的某个文本 属性 更改控件的背景(画笔)。
当我的转换器只有 2 个画笔属性时,我在一个更简单(有效)的示例中做到了这一点:
<local:ListErrorWarndescriptionToColorConverter x:Key="ErrorListToColor"
NormalBrush="Transparent" ErrorBrush="Red" WarnBrush="Yellow"/>
下一步是编写转换器来处理 2 个以上的字符串
我的转换器代码:
public class StringToBrushDictionary : Dictionary<string, Brush> { }
[ValueConversion(typeof(string), typeof(Brush))]
public sealed class TextToBrushConverter : IValueConverter
{
public StringToBrushDictionary LookUpTable { get; set; }
public Brush Default { get; set; }
public TextToBrushConverter()
{
// set defaults
LookUpTable = new StringToBrushDictionary();
Default = Brushes.Transparent;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var collection = value as string;
if (collection == null)
return Default;
if (LookUpTable.ContainsKey(collection))
return LookUpTable[collection];
return Default;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
我现在的问题是:如何在 xaml 中填写该词典 我找到了 this 8 year old question 但它并不是我所需要的,因为它使用字符串和 int,两个 "native" 数据类型并且还使用 VS 2010,它不支持字典(根据那里的答案)。那里的一个答案说后来的 XAML 版本可以做到这一点,但 VS 2010 还没有实现它。
这是我对转换器标记的看法以及我已经尝试过的内容:
<Application x:Class="BetterListbox.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BetterListbox"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:Specialized="clr-namespace:System.Collections.Specialized;assembly=System"
StartupUri="MainWindow.xaml">
<Application.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis" />
<local:ListErrorWarndescriptionToColorConverter x:Key="ErrorListToColor"
NormalBrush="Transparent" ErrorBrush="Red" WarnBrush="Yellow"/>
<local:TextToBrushConverter x:Key="WarnErrorToBrush" Default="Red">
<local:TextToBrushConverter.LookUpTable>
<local:StringToBrushDictionary>
<Brush x:Key="Error" >
...?
</Brush>
<collections:DictionaryEntry x:Key="d" Value="Brushes.Red" />
</local:StringToBrushDictionary>
</local:TextToBrushConverter.LookUpTable>
</local:TextToBrushConverter>
</Application.Resources>
</Application>
是否可以在 xaml 中填写 LookUpTable
,如果可以,如何填写?
你可以这样填写:
<my:TextToBrushConverter x:Key="textToBrush">
<my:TextToBrushConverter.LookUpTable>
<!-- LookUpTable["red"] = Brushes.Red -->
<x:Static MemberType="Brushes" Member="Red" x:Key="red" />
<!-- LookUpTable["aqua"] = new SolidColorBrush(Colors.Aqua) -->
<SolidColorBrush Color="Aqua" x:Key="aqua" />
<!-- custom color brush -->
<SolidColorBrush Color="#234433" x:Key="green" />
</my:TextToBrushConverter.LookUpTable>
</my:TextToBrushConverter>