Panel.Background
属性是 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}}"/>
System.Windows.Media.Color
的设置呢?只需在“设置类型”组合框中单击“浏览...”,然后选择PresentationCore -> System.Windows.Media -> Color即可。 - Clemens