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();
}
}
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()");
}
}
[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();
}
}
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();
}
}
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));
}
8条答案
按热度按时间2nbm6dog1#
我知道我真的迟到了,但你不需要一个转换器。
你可以
3pmvbmvn2#
看来你必须创建自己的转换器了。下面举一个简单的例子开始:
要使用它,请在resource-section中声明它。
并在绑定中将其作为静态资源使用:
我还没有测试过。如果它不工作,请发表评论。
hiz5n14c3#
此处不需要
Converter
。您可以在XAML
中定义Brush
并使用它。最好将Brush
定义为Resource
,以便在其他需要的地方使用它。XAML
如下所示:qrjkbowd4#
我想用HCL的方式而不是Jens的方式来做这个,因为我有很多东西绑定到
Color
,所以有更少的重复和样板.Fill
节点。然而,在尝试编写它时,ReSharper将我指向了WPF Toolkit的ColorToSolidColorBrushConverter实现。您需要在主Window/UserControl节点中包含以下名称空间声明:
然后在Window/UserControl资源中添加一个静态资源:
然后你可以像HCL的回答那样做:
7ivaypg95#
随着一些更多的增强HCL的答案,我测试了它-它的工作。
cu6pst1q6#
转换器:
XAML文件:
yvt65v4c7#
除HCL外,回答:如果您不想在意使用的是System.Windows.Media.Color还是System.Drawing.Color,您可以使用此转换器,它同时接受以下两种格式:
o2gm4chl8#
使用Pattern matching时,既不需要空值检查,也不需要双重转换:
ConvertBack的可选实现: