wpf static binding question

2019-06-26 16:35发布

Ok guys,

I have a serious problem with this.

I have a static class with static properties providing some colors as a hex string:

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; }
    }
}
}

Now, I want to bind these colors to a UserControl via XAML, like this:

<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}" 
/>>

I know that this does not work, so my question is, how am I doing it right? I don't need two-way-binding or any PropertyChanged events, since the colors will not be updated once the app is started.

1条回答
Summer. ? 凉城
2楼-- · 2019-06-26 17:13

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}"/>
查看更多
登录 后发表回答