WPF静态绑定问题

4

大家好,

我在这个问题上遇到了严重的问题。

我有一个静态类,其中的静态属性提供一些十六进制颜色字符串:

namespace com.myCom.Views
{
public static class MyColorTable
{
    private const string _Hex0 = "#FFFFFFFF";
    private const string _Hex1 = "#FFE5E5E5";

    public static String Hex0
    {
        get { return _Hex0; }
    }

    public static String Hex1
    {
        get { return _Hex1; }
    }
}
}

现在,我想通过XAML将这些颜色绑定到一个UserControl,就像这样:
<UserControl x:Class="com.testing.MyTestClass"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Height="53" Width="800" 
FocusVisualStyle="{x:Null}">
<Grid x:Name="MyGrid" 
Focusable="false" 
FocusManager.IsFocusScope="True" 
Background="{Binding Soure={x:Static MyColorTable}, Path=Hex1}" 
Margin="0,0,0,0" 
FocusVisualStyle="{x:Null}" 
/>>

我知道这样做是不起作用的,所以我的问题是,我该如何正确地做到这一点?我不需要双向绑定或任何PropertyChanged事件,因为一旦应用程序启动,颜色就不会更新。
1个回答

7

got it:

<UserControl x:Class="com.testing.MyTestClass"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:colors="clr-namespace:com.myCom.Views;assembly=com.myCom" 
             Height="53" Width="800" 
             FocusVisualStyle="{x:Null}">
    <Grid x:Name="MyGrid" 
          Focusable="false" 
          FocusManager.IsFocusScope="True" 
          Background="{Binding Source={x:Static Member=colors:MyColorTable.Hex1}}" 
          Margin="0,0,0,0" 
          FocusVisualStyle="{x:Null}"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接