如何在XAML中将颜色转换为画笔? [英] How do I convert a Color to a Brush in XAML?

查看:268
本文介绍了如何在XAML中将颜色转换为画笔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将System.Windows.Media.Color值转换为System.Windows.Media.Brush。颜色值是数据绑定到一个Rectangle对象的Fill属性。 Fill属性需要一个Brush对象,所以我需要一个IValueConverter对象来执行转换。



有一个内置的转换器在WPF或者我需要创建我的拥有?

解决方案

看来你必须创建自己的转换器。下面是一个简单的例子:

  public class ColorToSolidColorBrushValueConverter:IValueConverter {

public object Convert值,类型targetType,对象参数,System.Globalization.CultureInfo文化){
if(null == value){
return null;
}
//对于一个更复杂的转换器,检查targetType并相应地做出反应。
if(value is Color){
color color =(Color)value;
return new SolidColorBrush(color);
}
//如果你愿意,你可以在这里支持更多的源类型
//例如我抛出一个异常

类型type = value.GetType() ;
throw new InvalidOperationException(Unsupported type [+ type.Name +]);
}

public object ConvertBack(对象值,类型targetType,对象参数,System.Globalization.CultureInfo文化){
//如果需要,在这里你可以转换回来。检查它是否是刷(如果它的),
//获得它的颜色值并返回它。

throw new NotImplementedException();
}
}

要使用它,请在资源部分

 < local:ColorToSolidColorBrushValueConverter x:Key =ColorToSolidColorBrush_ValueConverter/> 

并且在绑定中使用它作为静态资源:

  Fill ={Binding Path = xyz,Converter = {StaticResource ColorToSolidColorBrush_ValueConverter}}

我没有测试它。如果它不工作,请发表评论。


I want to convert a System.Windows.Media.Color value to a System.Windows.Media.Brush. The color value is databound to a Rectangle object's Fill property. The Fill property takes a Brush object, so I need an IValueConverter object to perform the conversion.

Is there a built-in converter in WPF or do I need to create my own? How do I go about creating my own if it becomes necessary?

解决方案

It seems that you have to create your own converter. Here a simple example to start:

public class ColorToSolidColorBrushValueConverter : IValueConverter {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            if (null == value) {
                return null;
            }
            // For a more sophisticated converter, check also the targetType and react accordingly..
            if (value is Color) {
                Color color = (Color)value;
                return new SolidColorBrush(color);
            }
            // You can support here more source types if you wish
            // For the example I throw an exception

            Type type = value.GetType();
            throw new InvalidOperationException("Unsupported type ["+type.Name+"]");            
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            // If necessary, here you can convert back. Check if which brush it is (if its one),
            // get its Color-value and return it.

            throw new NotImplementedException();
        }
    }

To use it, declare it in the resource-section.

 <local:ColorToSolidColorBrushValueConverter  x:Key="ColorToSolidColorBrush_ValueConverter"/>

And the use it in the binding as a static resource:

  Fill="{Binding Path=xyz,Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"

I have not tested it. Make a comment if its not working.

这篇关于如何在XAML中将颜色转换为画笔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆