绑定到静态属性在WPF静态类 [英] Binding to static property in static class in 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..
推荐答案
问题是,你的源属性的<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.media.color%28v=vs.110%29.aspx\"><$c$c>Color$c$c>类型和目的地属性是<一href=\"http://msdn.microsoft.com/en-us/library/system.windows.media.brush%28v=vs.110%29.aspx\"><$c$c>Brush$c$c>.您可以创建<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.media.solidcolorbrush.color%28v=vs.110%29.aspx\"><$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>
这篇关于绑定到静态属性在WPF静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!