WPF的转换器中使用Brush应注意问题

1、Brushes用错了命名空间将导致程序无法正确工作。笔者曾写了一个flag值转换为相应的Brush,由于不小心Using了一个错误的命名空间(System.Drawing)导致Xaml文件界面元素Fill绑定属性总出不来应有的效果。最后费了好大劲才发现,替换为正确的命名空间(System.Windows.Media)后立马就解决了。特此留下随笔备忘。

2、下面是一个IMultiValueConverter的例子:

<Window x:Class="WpfApplication16.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:my="clr-namespace:WpfApplication16" 
    Title="Window1" Height="300" Width="300"> 
    <Window.Resources> 
        <my:RgbConverter x:Key="RgbConverter" /> 
    </Window.Resources> 
    <Window.Background> 
        <SolidColorBrush> 
            <SolidColorBrush.Color> 
                <MultiBinding Converter="{StaticResource RgbConverter}"> 
                    <Binding Path="Value" ElementName="redSlider" /> 
                    <Binding Path="Value" ElementName="greenSlider" /> 
                    <Binding Path="Value" ElementName="blueSlider" /> 
                </MultiBinding> 
            </SolidColorBrush.Color> 
        </SolidColorBrush> 
    </Window.Background> 
    <StackPanel> 
        <Slider Minimum="0" Maximum="255" x:Name="redSlider" /> 
        <Slider Minimum="0" Maximum="255" x:Name="greenSlider" /> 
        <Slider Minimum="0" Maximum="255" x:Name="blueSlider" /> 
    </StackPanel> 
</Window> 
public class RgbConverter : IMultiValueConverter 
{ 
    #region IMultiValueConverter Members 
 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
        var r = System.Convert.ToByte(values[0]); 
        var g = System.Convert.ToByte(values[1]); 
        var b = System.Convert.ToByte(values[2]); 
 
        return Color.FromRgb(r, g, b); 
    } 
 
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    { 
        throw new NotImplementedException(); 
    } 
 
    #endregion 
} 

 3、多值参数如何处理,譬如:希望value为真时返回ConvertParameter指定R,G,B的颜色值Brush,否则返回透明Brush。

<Border Grid.ColumnSpan="3" BorderBrush="{Binding Converter={StaticResource BoolToSolidColorBrushConverter}, ConverterParameter='255,0,0', Path=IsInclusive}" BorderThickness="2">
    <Image Source="{Binding Image}" Opacity="{Binding ImageOpacity}" />
</Border>
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    Brush result = Brushes.Red;
    if (parameter != null)
    {
        string[] rgb = parameter.ToString().Split(',');
        if (rgb.Length == 3)
        {
            byte r = 0;
            byte.TryParse(rgb[0], out r);
            byte g = 0;
            byte.TryParse(rgb[1], out g);
            byte b = 0;
            byte.TryParse(rgb[2], out b);

            result = new SolidColorBrush(Color.FromRgb(r, g, b));
        }
    }

    if (value == null) return Brushes.Transparent;

    string valueString = value.ToString();
    if (value.ToString() == "1")
    {
        valueString = "True";
    }
    else if (value.ToString() == "0")
    {
        valueString = "False";
    }

    bool boolValue = false;
    if (!bool.TryParse(valueString, out boolValue))
    {
        boolValue = false;
    }
    if (boolValue) return result;

    return Brushes.Transparent;
}
原文地址:https://www.cnblogs.com/chriskwok/p/2644201.html