【问题标题】:Segue Icon in TextBlock with Data Binding带有数据绑定的 TextBlock 中的 Segue 图标
【发布时间】:2015-02-23 15:10:50
【问题描述】:

我正在尝试制作一个带有图标的网格。我将图标的字符串表示形式存储在数据库中,并尝试通过绑定到我的视图模型上的 Icon 属性(字符串)在 GridHub 中显示这些图标。

当我使用这个 xaml 时,我得到了显示在网格中的实际文本

<TextBlock Text="{Binding Icon}"
    FontFamily="Segoe UI Symbol"
    FontSize="100"
    TextAlignment="Center"
    VerticalAlignment="Top"
    Foreground="DarkGray"
    AutomationProperties.Name="Some Label"/>

但这会按预期显示图标

<TextBlock Text="&#xE1D4;"
    FontFamily="Segoe UI Symbol"
    FontSize="100"
    TextAlignment="Center"
    VerticalAlignment="Top"
    Foreground="DarkGray"
    AutomationProperties.Name="Some Label"/>

我的模型看起来像:

 public class PermissionGroup
{
    /// <summary>
    /// The unique identifier for the group.
    /// </summary>
    public string PermissionGroupId { get; set; }
    /// <summary>
    /// The name of the group
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// The page associated with the permission group
    /// </summary>
    public string PageName { get; set; }

    /// <summary>
    /// The icon that will be shown on the PermissionHub page
    /// </summary>
    public string Icon { get; set; }

    /// <summary>
    /// A description of the permissions in the group
    /// </summary>
    public string Description { get; set; }

    /// <summary>
    /// The permissions associated with this permission group
    /// </summary>
    public List<Permission> Permissions { get; set; } 
}

我的视图模型只包含这些对象的列表。

在我的数据库中,我尝试存储以下内容:

  1. &amp;#xE1D4;
  2. \uE1D4(必须转义才能进入数据库)
  3. 57812(char.Parse("\uE1D4")的结果)

这些都没有导致图标正确显示!

The output of first XAML example

【问题讨论】:

    标签: c# xaml windows-8.1


    【解决方案1】:

    您必须解码 html 编码的字符串。例如,你可以试试这个非常粗略的例子:

    public class ViewModel
        {
            public PermissionGroup PG {get; set;}
            public ViewModel()
            {
                PG = new PermissionGroup();
                PG.Icon= System.Net.WebUtility.HtmlDecode("&#xE1D4;");
            }
        }
    
    
    
    
    public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
                this.DataContext = new ViewModel();
            }
        }
    
    
    <TextBlock Text="{Binding Path=PG.Icon}" FontFamily="Segoe UI Symbol"/>
    

    或

    <Style x:Key="Segoe">
        <Setter x:Name="Segoe" Property="TextElement.FontFamily" Value="Resources/#Segoe UI Symbol" />
    </Style>
    
    <TextBlock Text="{Binding PG.Icon}"
                Style="{DynamicResource Segoe}"
    

    可以使用字体查看器here,以备不时之需。

    编辑:无论如何,unicode 转义序列应该比 html 编码更好,这就是为什么我建议使用字体查看器仔细检查并将字体 ttf 作为资源链接的原因。 您还可以添加一个按钮来分析您的 xaml 文本的 unicode 转换,当直接输入时无需绑定。像这样的

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        byte[] stringBytes = Encoding.Unicode.GetBytes(txname.Text);
        char[] stringChars = Encoding.Unicode.GetChars(stringBytes);
        StringBuilder builder = new StringBuilder();
        Array.ForEach<char>(stringChars, c => builder.AppendFormat("\\u{0:X}", (int)c));
        Debug.WriteLine(builder);
    } 
    

    【讨论】:

    • 谢谢 Giulio,我会试试的。我不确定 HTML 解码是否正确,因为我没有创建 HTML(它的 XAML),但我会看看是否有我可以使用的解码机制。谢谢。
    • 我在发布它们之前已经尝试过这些代码行,当然 :-) 除了那个特定的字符串和关于你的评论/疑问,现在我还找到了来自 msdn 的这个外部链接(关于 xaml ) 这似乎证实了我的提示:social.msdn.microsoft.com/Forums/silverlight/en-US/…
    • 你也可以看看this关于在xaml中包含外部字体的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    相关资源
    最近更新 更多