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

eoigrqb6  于 2022-12-07  发布在  其他
关注(0)|答案(8)|浏览(104)

我要将System.Windows.Media.Color值转换为System.Windows.Media.Brush。色彩值会数据系结至Rectangle对象的Fill属性。Fill属性会接受Brush对象,因此我需要IValueConverter对象来执行转换。
WPF中是否有内置的转换器,或者我需要创建自己的转换器?如果有必要,我如何创建自己的转换器?

2nbm6dog

2nbm6dog1#

我知道我真的迟到了,但你不需要一个转换器。
你可以

<Rectangle>
    <Rectangle.Fill>
        <SolidColorBrush Color="{Binding YourColorProperty}" />
    </Rectangle.Fill>
</Rectangle>
3pmvbmvn

3pmvbmvn2#

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

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

要使用它,请在resource-section中声明它。

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

并在绑定中将其作为静态资源使用:

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

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

hiz5n14c

hiz5n14c3#

此处不需要Converter。您可以在XAML中定义Brush并使用它。最好将Brush定义为Resource,以便在其他需要的地方使用它。
XAML如下所示:

<Window.Resources>
    <SolidColorBrush Color="{Binding ColorProperty}" x:Key="ColorBrush" />
</Window.Resources>
<Rectangle Width="200" Height="200" Fill="{StaticResource ColorBrush}" />
qrjkbowd

qrjkbowd4#

我想用HCL的方式而不是Jens的方式来做这个,因为我有很多东西绑定到Color,所以有更少的重复和样板.Fill节点。
然而,在尝试编写它时,ReSharper将我指向了WPF Toolkit的ColorToSolidColorBrushConverter实现。您需要在主Window/UserControl节点中包含以下名称空间声明:

xmlns:Toolkit="clr-namespace:Microsoft.Windows.Controls.Core.Converters;assembly=WPFToolkit.Extended"

然后在Window/UserControl资源中添加一个静态资源:

<Toolkit:ColorToSolidColorBrushConverter x:Key="colorToSolidColorBrushConverter" />

然后你可以像HCL的回答那样做:

<Rectangle Fill="{Binding Color, Converter={StaticResource colorToSolidColorBrushConverter}}" />
7ivaypg9

7ivaypg95#

随着一些更多的增强HCL的答案,我测试了它-它的工作。

public class ColorToSolidColorBrushValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return null;

        if (value is Color)
            return new SolidColorBrush((Color)value);

        throw new InvalidOperationException("Unsupported type [" + value.GetType().Name + "], ColorToSolidColorBrushValueConverter.Convert()");
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return null;

        if (value is SolidColorBrush)
            return ((SolidColorBrush)value).Color;

        throw new InvalidOperationException("Unsupported type [" + value.GetType().Name + "], ColorToSolidColorBrushValueConverter.ConvertBack()");
    }

}
cu6pst1q

cu6pst1q6#

转换器:

[ValueConversion(typeof(SolidColorBrush), typeof(Color))]
public class SolidBrushToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is SolidColorBrush)) return null;
        var result = (SolidColorBrush)value;
        return result.Color;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML文件:

//...
<converters:SolidBrushToColorConverter x:Key="SolidToColorConverter" />
//...
<Color>
    <Binding Source="{StaticResource YourSolidColorBrush}"
             Converter="{StaticResource SolidToColorConverter}">
    </Binding>
</Color>
//...
yvt65v4c

yvt65v4c7#

除HCL外,回答:如果您不想在意使用的是System.Windows.Media.Color还是System.Drawing.Color,您可以使用此转换器,它同时接受以下两种格式:

public class ColorToSolidColorBrushValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        switch (value)
        {
            case null:
                return null;
            case System.Windows.Media.Color color:
                return new SolidColorBrush(color);
            case System.Drawing.Color sdColor:
                return new SolidColorBrush(System.Windows.Media.Color.FromArgb(sdColor.A, sdColor.R, sdColor.G, sdColor.B));
        }

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

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
o2gm4chl

o2gm4chl8#

使用Pattern matching时,既不需要空值检查,也不需要双重转换:

public class ColorToSolidBrushValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is Color color) return new SolidColorBrush(color);

        throw new InvalidOperationException(nameof(color));
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => new NotImplementedException();
}

ConvertBack的可选实现:

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (value is SolidColorBrush brush)
    {
        return brush.Color;
    }

    throw new InvalidOperationException(nameof(brush));
}

相关问题