wpf static binding question

雨燕双飞 提交于 2020-01-24 07:32:31

问题


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:


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


来源:https://stackoverflow.com/questions/2371281/wpf-static-binding-question

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!