C#自定义字体值被解释为String而不是Single Value

时间:2016-03-31 18:13:29

标签: c# wpf

我正在使用自定义字体在我的WPF应用程序中显示图标。我写了一个小的演示应用程序来向我展示图标以及相应的“角色”。

不幸的是,C#不会将我的迭代结果字符串解释为单个字符,而是将其解释为字符串。

代码:

 private string _BaseString = "&#xE";
        public IconsPane()
        {
            InitializeComponent();
            InitIcons();
        }

        private void InitIcons()
        {

            string[] hex = new string[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

            string output = "";
            foreach (string a in hex)
            {
                foreach (string b in hex)
                {
                    foreach (string c in hex)
                    {
                        output = a + b + c;
                        CreateIcon(output);
                    }
                }

            }

        }

        private void CreateIcon(string output)
        {
            StackPanel st = new StackPanel();
            st.Margin = new Thickness(5);

            Button ILabel = new Button();
            ILabel.Style = (Style)FindResource("ContentButtonPlain");
            ILabel.Content = _BaseString + output + ";";

            TextBox IDesc = new TextBox();
            IDesc.Text = (string)ILabel.Content;
            IDesc.HorizontalAlignment = HorizontalAlignment.Center;

            st.Children.Add(ILabel);
            st.Children.Add(IDesc);

            IconDashboard.Children.Add(st);

        }

输出: enter image description here

相反,我希望每个Icon都在该字符串后面:

预期产出:

enter image description here

预期输出的屏幕截图取自设计器中的XAML代码:

<Button Margin="5 0 5 0"
                                                        Style="{StaticResource ContentButtonPlain}"
                                                        Content="&#xE3e2;" />

在这种情况下,图标以正确的方式识别。

1 个答案:

答案 0 :(得分:0)

您似乎想要在E000EFFF范围内创建所有符号。你可以这样做:

foreach (char symbolChar in Enumerable.Range(0xE000, 0x1000))
{
    var symbolString = new string(symbolChar, 1);

    // replace this by your actual code that creates UI elements
    st.Children.Add(new Label { Content = symbolString });
}

除此之外,您最好使用带有适当ItemTemplate的ItemsControl来显示符号,如下所示:https://stackoverflow.com/a/34156141/1136211