C# 文本块中带数据绑定的Segue图标

C# 文本块中带数据绑定的Segue图标,c#,xaml,windows-8.1,C#,Xaml,Windows 8.1,我正在尝试制作一个带有图标的网格。我将图标的字符串表示形式存储在数据库中,并尝试通过绑定到viewmodel上的图标属性(字符串)在GridHub中显示这些图标 当我使用这个xaml时,我得到了网格中显示的实际文本 <TextBlock Text="{Binding Icon}" FontFamily="Segoe UI Symbol" FontSize="100" TextAlignment="Center" VerticalAlignment="Top"

我正在尝试制作一个带有图标的网格。我将图标的字符串表示形式存储在数据库中,并尝试通过绑定到viewmodel上的图标属性(字符串)在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; } 
}
公共类权限组
{
/// 
///组的唯一标识符。
/// 
公共字符串PermissionGroupId{get;set;}
/// 
///组的名称
/// 
公共字符串名称{get;set;}
/// 
///与权限组关联的页面
/// 
公共字符串PageName{get;set;}
/// 
///将显示在PermissionHub页面上的图标
/// 
公共字符串图标{get;set;}
/// 
///组中权限的说明
/// 
公共字符串说明{get;set;}
/// 
///与此权限组关联的权限
/// 
公共列表权限{get;set;}
}
我的viewmodel只包含这些对象的列表

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

  • 和#xE1D4
  • \uE1D4(必须转义才能进入数据库)
  • 57812(char.Parse(“\uE1D4”)的结果)
  • 所有这些都没有导致图标正确显示


    您必须解码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"/>
    
    公共类视图模型
    {
    公共许可组PG{get;set;}
    公共视图模型()
    {
    PG=新许可组();
    PG.Icon=System.Net.WebUtility.HtmlDecode(;);
    }
    }
    公共部分类Window1:Window
    {
    公共窗口1()
    {
    初始化组件();
    this.DataContext=newviewmodel();
    }
    }
    

    
    builder.AppendFormat(“\\u{0:X}”,(int)c));
    Debug.WriteLine(builder);
    } 
    
    谢谢朱利奥,我会尝试一下。我不确定HTML解码是否正确,因为我没有创建HTML(它的XAML),但我会看看是否有一种解码机制可以使用。谢谢。在发布这些代码之前,我已经试过了,当然:-)除了那个特定的字符串和关于您的评论/疑问,现在我还发现了来自msdn(关于xaml)的这个外部链接,它似乎证实了我的提示:您还可以看看关于在xaml中包含外部字体的答案
    <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}"
    
    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);
    }