【问题标题】:Binding StaticResourse Key in uwp在 uwp 中绑定静态资源键
【发布时间】:2017-06-15 03:32:40
【问题描述】:

我正在尝试使用一些 FontAwesome 图标重新修复汉堡菜单,我的方法是在我的应用程序中使用 ResourseDictionoary。现在我想为下面的字形绑定 keyFontAwesomeUserString。我在对象中的属性是类型为string 的图标。在我的列表中,x:DataType="local:MenuItem" 的 Icon var 具有我的资源词典中的值。

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

请告诉我是否/如何绑定静态资源的 ResourceKey 属性。 谢谢

【问题讨论】:

  • 您还需要发布您的资源代码。你在这里做得对。请记住 {StaticResource} 与 {Binding} 不同,但对于您所做的,这看起来是正确的。
  • 嗨@MichaelPuckettII。感谢您的回答。您可以找到源代码here,我希望静态资源从代码behind 中获取所有值,这是我在xaml 中的代码
  • 谢谢阿纳斯塔西奥斯。然而;下面的文森特已经回答了这个问题,就像我会给予或接受一样,所以看看他在那里说了什么。

标签: c# windows xaml binding uwp


【解决方案1】:

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

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

不要忘记StaticResource 仅在页面创建时被读取。

根据您更新字典的时间,这可能就足够了,但如果您希望应用程序在您更改资源字典中的某些内容时正确更新自身,则必须使用ThemeResource

您可以获取更多关于ThemeResourcehere的详细信息。

<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

【讨论】:

  • 感谢您的回答,我看不出如何在对象 like this 中实现此功能以进行绑定。我希望它采用不同的值。不仅是“FontAwesomeUserString”值。
猜你喜欢
  • 1970-01-01
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
  • 1970-01-01
相关资源
最近更新 更多