如何在XAML中将颜色转换为画笔?

dth*_*her 49 data-binding wpf xaml colors converter

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

WPF中是否有内置转换器,还是需要创建自己的转换器?如果有必要,我该如何创建自己的?

Jen*_*ens 130

我知道我参加派对的时间已经很晚了,但你不需要转换器.

你可以做到

<Rectangle>
    <Rectangle.Fill>
        <SolidColorBrush Color="{Binding YourColorProperty}" />
    </Rectangle.Fill>
</Rectangle>
Run Code Online (Sandbox Code Playgroud)

  • 虽然我喜欢这个解决方案,但确实会产生如此处所述的烦人警告:http://stackoverflow.com/questions/7926204/cannot-find-governing-frameworkelement-warning-when-binding-inside-datatemp (4认同)
  • 这个解决方案比接受的答案要好得多.谢谢! (2认同)

HCL*_*HCL 63

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

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

要使用它,请在资源部分中声明它.

<local:ColorToSolidColorBrushValueConverter  x:Key="ColorToSolidColorBrush_ValueConverter"/>
Run Code Online (Sandbox Code Playgroud)

并将其作为静态资源用于绑定:

Fill="{Binding Path=xyz,Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"
Run Code Online (Sandbox Code Playgroud)

我没有测试过.如果它不起作用,请发表评论.


Jac*_*ope 7

我想用HCL的方式而不是Jens的方式,因为我有很多事情要束缚Color,所以重复和锅炉板.Fill节点更少.

但是在尝试编写时,ReSharper向我指出了WPF Toolkit对ColorToSolidColorBrushConverter的实现.您需要在主Window/UserControl节点中包含以下名称空间声明:

xmlns:Toolkit="clr-namespace:Microsoft.Windows.Controls.Core.Converters;assembly=WPFToolkit.Extended"
Run Code Online (Sandbox Code Playgroud)

然后是Window/UserControl资源中的静态资源:

<Toolkit:ColorToSolidColorBrushConverter x:Key="colorToSolidColorBrushConverter" />
Run Code Online (Sandbox Code Playgroud)

那么你可以像HCL的答案那样做:

<Rectangle Fill="{Binding Color, Converter={StaticResource colorToSolidColorBrushConverter}}" />
Run Code Online (Sandbox Code Playgroud)


Kyl*_*Ren 6

Converter这里不需要A。您可以定义一个Brushin XAML并使用它。最好将Brushas 定义为a,Resource以便可以在其他需要的地方使用。

XAML 如下:

<Window.Resources>
    <SolidColorBrush Color="{Binding ColorProperty}" x:Key="ColorBrush" />
</Window.Resources>
<Rectangle Width="200" Height="200" Fill="{StaticResource ColorBrush}" />
Run Code Online (Sandbox Code Playgroud)