我想将System.Windows.Media.Color值转换为System.Windows.Media.Brush。颜色值是数据绑定到Rectangle对象的填充属性。 Fill属性需要一个Brush对象,所以我需要一个IValueConverter对象来执行转换。如何将颜色转换为XAML中的画笔?
在WPF中是否有内置的转换器,还是我需要创建自己的?如果有必要,我该如何创建自己的?
我想将System.Windows.Media.Color值转换为System.Windows.Media.Brush。颜色值是数据绑定到Rectangle对象的填充属性。 Fill属性需要一个Brush对象,所以我需要一个IValueConverter对象来执行转换。如何将颜色转换为XAML中的画笔?
在WPF中是否有内置的转换器,还是我需要创建自己的?如果有必要,我该如何创建自己的?
看来你必须创建你自己的转换器。这里有一个简单的例子:
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();
}
}
要使用它,请在资源部分声明它。
<local:ColorToSolidColorBrushValueConverter x:Key="ColorToSolidColorBrush_ValueConverter"/>
而且使用它在绑定的静态资源:
Fill="{Binding Path=xyz,Converter={StaticResource ColorToSolidColorBrush_ValueConverter}}"
我没有测试它。如果它不起作用,请发表评论。
感谢一群@Chris Valentine。尽管Jens的回答非常优雅,但在我的情况下,我无法使用它,所以我不得不使用Converter。或者我可能看不到如何在我的情况下实现SolidColorBrush。 – philologon 2014-05-24 18:14:45
我知道我真的很晚参加派对,但是你不需要这个转换器。
你可以做
<Rectangle>
<Rectangle.Fill>
<SolidColorBrush Color="{Binding YourColorProperty}" />
</Rectangle.Fill>
</Rectangle>
该解决方案比接受的答案好得多。谢谢! – 2012-11-02 17:24:07
虽然我喜欢这个解决方案,它确实导致恼人的警告如下所述:http://stackoverflow.com/questions/7926204/cannot-find-governing-frameworkelement-warning-when-binding-inside-datatemp – l33t 2014-05-22 09:49:17
我想,因为我有很多东西绑定到Color
为此HCL的方式,而不是延的方式,所以有较少的重复和锅炉板.Fill
节点。
然而,当试图编写它时,ReSharper指出我对ColorToSolidColorBrushConverter的实现是WPF Toolkit。你需要在主窗口/用户控件节点下面的命名空间声明:
xmlns:Toolkit="clr-namespace:Microsoft.Windows.Controls.Core.Converters;assembly=WPFToolkit.Extended"
然后在窗口/用户控件资源静态资源:
<Toolkit:ColorToSolidColorBrushConverter x:Key="colorToSolidColorBrushConverter" />
然后,你可以不喜欢它HCL的回答:
<Rectangle Fill="{Binding Color, Converter={StaticResource colorToSolidColorBrushConverter}}" />
随着一些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()");
}
}
发布后它..我注意到延斯回答..这是准确的:) – 2013-02-24 15:52:43
转换器:
[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>
//...
不在这里需要一个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}" />
可能重复:http://stackoverflow.com/questions/372693/convert-string-to-brushes-brush-name-in-c – 2010-07-22 14:11:12
我不认为这是重复的。我想从Color对象转换,而不是像“Red”这样的字符串值。我想将它作为XAML转换器来实现,而不是在后面的C#代码中执行转换。 – dthrasher 2010-07-22 14:28:33