绑定到静态属性在WPF静态类

问题描述:

我有一个问题,从静态的类结合静态属性的值。

I have a problem with binding values from static properties from static class.

我的类:

namespace MyNamespace.Data
{
    public static class MySettings
    {
        public static Color BackgroundColor { get; set; }
        public static Color FontColor { get; set; }
    }
}

XAML:

<Page ...
       xmlns:colors="clr-namespace:MyNamespace.Data"
      ...>
 ...
<Button Grid.Column="0" Content="Text"
        Background="{Binding Source={x:Static s:MySettings.BackgroundColor}}"
        Foreground="{Binding Source={x:Static s:MySettings.FontColor}}"
        BorderBrush="{Binding Source={x:Static s:MySettings.FontColor}}"/>

当我运行此code背景设置确定,但其余保持不变。

and when I run this code Background is set OK but the rest remains unchanged..

问题是,你的源属性的Color$c$c>类型和目的地属性是Brush$c$c>.您可以创建SolidColorBrush$c$c>用你的颜色,像这样:

Problem is that your source properties are of a Color type and destination properties are Brush. You can create SolidColorBrush using your color like so:

<Button Content="Text">
    <Button.Background>
        <SolidColorBrush Color="{Binding Source={x:Static s:MySettings.BackgroundColor}}"/>
    </Button.Background>
    <Button.Foreground>
        <SolidColorBrush Color="{Binding Source={x:Static s:MySettings.FontColor}}"/>
    </Button.Foreground>
    <Button.BorderBrush>
        <SolidColorBrush Color="{Binding Source={x:Static s:MySettings.FontColor}}"/>
    </Button.BorderBrush>
</Button>