简体   繁体   English

用数据绑定在TextBlock中查看图标

[英]Segue Icon in TextBlock with Data Binding

I am trying to make a Grid with an Icon in it. 我正在尝试制作一个带有图标的网格。 I am storing the string representation of the icon in a database, and trying to display these icons in a GridHub by binding to the Icon property (a string) on my viewmodel. 我将图标的字符串表示存储在数据库中,并尝试通过绑定到我的viewmodel上的Icon属性(字符串)在GridHub中显示这些图标。

When I use this xaml, I get the actual text displayed in the grid 当我使用这个xaml时,我得到网格中显示的实际文本

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

But this displays the icon as expected 但是这会按预期显示图标

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

My Model looks like: 我的模型看起来像:

 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; } 
}

and my viewmodel just contains a list of these objects. 我的viewmodel只包含这些对象的列表。

In my database I have tried storing the following: 在我的数据库中,我尝试存储以下内容:

  1. &#xE1D4;
  2. \ (has to be escaped to get in the db) \\ uE1D4(必须转义才能进入数据库)
  3. 57812 (the result of char.Parse("\") ) 57812( char.Parse("\")

and none of these has resulted in the icon being displayed correctly! 并且这些都没有导致图标正确显示!

The output of first XAML example 第一个XAML示例的输出

You have to decode the html encoded string. 您必须解码html编码的字符串。 For instance you can try this very rough example: 例如,您可以尝试这个非常粗略的例子:

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

or 要么

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

A font viewer is available here , in case you need one. A字型观众可在这里 ,如果你需要一个。

EDIT: anyway the unicode escape sequence should work better than the html encoding, that's why I'm suggesting to double check with a font viewer and to link the font ttf as a resource. 编辑:无论如何,unicode转义序列应该比html编码更好,这就是为什么我建议用字体查看器仔细检查并将字体ttf链接为资源。 You can also add a button to analyse the unicode conversion of your xaml text, when directly entered without binding. 您还可以添加一个按钮来分析xaml文本的unicode转换,直接输入时不进行绑定。 Something like this 像这样的东西

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);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM