Panel.Background
property 是一种System.Windows.Media.Brush
类型,System.Windows.Media.Color
因此您需要将其转换为SolidColorBrush
. 您可以在下面找到这两种情况:
设置是System.Windows.Media.Color
类型
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="{Binding Source={x:Static props:Settings.Default}, Path=Bkg}"/>
</Setter.Value>
</Setter>
设置是System.Drawing.Color
类型:为此,您需要自定义IValueConverter
将其转换为SolidColorBrush
:
public class ColorToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var dc = (System.Drawing.Color)value;
return new SolidColorBrush(new Color { A = dc.A, R = dc.R, G = dc.G, B = dc.B });
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
您在资源中定义的:
<Window.Resources>
<local:ColorToBrushConverter x:Key="ColorToBrushConverter"/>
</Window.Resources>
你可以像这样使用它:
<Setter Property="Background" Value="{Binding Source={x:Static props:Settings.Default}, Path=Bkg, Converter={StaticResource ColorToBrushConverter}}"/>