将TextBlock前景绑定到xaml中的Global.cs变量

时间:2012-07-25 10:31:06

标签: xaml binding silverlight-5.0

我一直在网上搜寻试图找到类似的问题但找不到任何可以解决我问题的东西。

我有一个包含全局变量的静态类,其中一些是我希望能够在我的xaml控件中绑定的颜色变量,这样我只需要更改一个变量来更改批次(全局变量的原因)

类名称空间为MyApp.Assets.Resources,类名为Global(。cs)。 在我的班级中,我有一些名为DEFAULTCOLOROKCOLORALARMCOLOR的变量。

我的xaml:

<UserControl
    <!-- ... -->
    xmlns:custom="clr-namespace:InspectionDB.Assets.Resources"
>
    <UserControl.Resources>
        <custom:Global x:Name="global"/> <!-- THIS GIVES AN ERROR SEE BELOW-->

        <common:HierarchicalDataTemplate x:Key="Level1" ItemsSource="{Binding Path=Children}">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Type, Converter={StaticResource localTreeImgConverter}}"/>
                <TextBlock {Binding...} />
                <StackPanel Orientation="Horizontal" Visibility="{Binding Type, Converter={StaticResource localStatusVisibilityConverter}}">
                    <TextBlock Margin="5,0, 0,0" Text="[ " Foreground="{Binding DEFAULTCOLOR, Source={StaticResource global}}" />
                    <TextBlock Text="{Binding Critical}" Foreground="{Binding ALARMCOLOR, Source={StaticResource global}}"/>
                    <TextBlock Text=" | " Foreground="{Binding DEFAULTCOLOR, Source={StaticResource global}}"/>
                    <TextBlock Text="{Binding OK}" Foreground="{Binding OKCOLOR, Source={StaticResource global}}"/>
                    <TextBlock Text=" ]" Foreground="{Binding DEFAULTCOLOR, Source={StaticResource global}}"/>
                </StackPanel>
            </StackPanel>
        </common:HierarchicalDataTemplate>

    </UserControl.Resources>

    <Grid>
        <!-- Where the hierarchical template is used --> 
    </Grid>

</UserControl>

参考资料中的部分会抛出错误以及绑定:

Unable to cast object of type 'ApplicationProxy' to type 'Microsoft.Expression.DesignModel.DocumentModel.DocumentNode'.

我的代码有什么问题,如何让它运行?

1 个答案:

答案 0 :(得分:0)

我发现我的问题是什么,因此我找不到任何类似的情况。

我有public class Global : App - 不要问我为什么,我很确定很久以前就有一个合理的理由,但这意味着它有一些时髦的循环参考。

问题解决了。