在uwp中绑定StaticResourse键

时间:2017-01-30 01:53:43

标签: c# windows xaml binding uwp

我正在尝试使用一些FontAwesome图标重新设置汉堡包菜单,我的方法是在我的应用程序中使用ResourseDictionoary。现在我想为下面的字形绑定密钥FontAwesomeUserString。对象中的我的属性是类型为string的Icon。在我的列表中,x:DataType="local:MenuItem"的Icon var具有来自我的resoursedictionary的值。

<FontIcon Grid.Column="0" FontFamily="{StaticResource FontAwesomeFontFamily}" Glyph="{StaticResource FontAwesomeUserString}" Foreground="White" />
<TextBlock Grid.Column="1" Text="{x:Bind Name, Mode=OneWay}" TextWrapping="Wrap" FontSize="16" VerticalAlignment="Center" Foreground="White"/>

请告诉我是否/如何绑定StaticResourse的ResourceKey属性。 谢谢

1 个答案:

答案 0 :(得分:1)

您可以通过以下代码替换资源字典来更改它们:

Application.Current.Resources["FontAwesomeUserString"] = "&glyphCode";

不要忘记只有在创建页面时才会读取StaticResource

取决于您更新字典时,它可能已足够,但如果您希望应用程序在更改资源字典中的某些内容时正确更新,则必须使用ThemeResource

您可以详细了解ThemeResource here

<FontIcon Grid.Column="0" 
    FontFamily="{ThemeResource FontAwesomeFontFamily}" 
    Glyph="{ThemeResource FontAwesomeUserString}" 
    Foreground="White" />

更新

如果您只是想为所有项目设置字形/字体系列,那么常规绑定就足够了:

<DataTemplate x:Key="DefaultTemplate" x:DataType="local:MenuItem">
        <Grid Width="240" Height="48">
           <Grid.ColumnDefinitions>
                <ColumnDefinition Width="48" />
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <FontIcon Grid.Column="0" FontFamily="{x:Bind FontFamily}" Glyph="{x:Bind Icon}" Foreground="White" />
            <TextBlock Grid.Column="1" Text="{x:Bind Name, Mode=OneWay}" TextWrapping="Wrap" FontSize="16" VerticalAlignment="Center" Foreground="White"/>
        </Grid>
    </DataTemplate>        

您只需在视图模式中定义FontFamilyIcon即可。 埃尔 您可以查看UWP工具包documentation

中的汉堡菜单