ListBox 中的绑定符号 [英] Binding Symbols in a ListBox

查看:25
本文介绍了ListBox 中的绑定符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 Segoe MDL2 Assets 中的一些项目用作从集合驱动的菜单的图标.

I want to use some items in Segoe MDL2 Assets as icons for a menu driven off a collection.

我已经定义了我的收藏

NavItems = new ObservableCollection<NavItem>
{
    new NavItem {Title = "Sign in", Icon="&#xE1E2;", ClassType = null },
    new NavItem {Title = "Settings", Icon="&#xE115;", ClassType = typeof(Settings) }
};

在我的 xaml 中,我有一个列表框定义为

In my xaml I have a listbox defined as

<ListBox ItemsSource="{Binding NavItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Height="24">
                <ContentControl Content="{Binding Icon}" FontFamily="Segoe MDL2 Assets" />
                <StackPanel Margin="20,0,0,0">
                    <TextBlock Text="{Binding Title}"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

问题是在呈现此列表框时,图标无法正确呈现(它们显示为一堆方块).但是,如果我直接在 xaml 中用图标值替换绑定语句,它就可以工作.我很好奇我将如何正确地进行这种绑定.

The problem is when this listbox is rendered, the icons don't render properly (they show up as a bunch of squares). However if I replace the binding statement with the icon value directly in the xaml it works. I'm curious on how I would do this binding correctly.

(请注意,除了具有相同结果的通用内容控件之外,我还尝试了各种控件,所以我知道它与此无关)

(note I've tried various controls besides a generic content control with the same results so I know it's unrelated to that)

非常感谢!

推荐答案

在 C# 中,unicode 字符转义序列\u 为前缀,因此您的代码应如下所示:

In C#, unicode character escapes sequences are prefixed by \u, so your code should look like this:

NavItems = new ObservableCollection<NavItem>
{
    new NavItem { Icon = "\uE1E2", ... },
    new NavItem { Icon = "\uE115", ... }
};

这篇关于ListBox 中的绑定符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆