c# - WinRT 应用程序 : ComboxBox and ListBox does not display anything when ItemsSource is List<Type>

标签 c# xaml combobox listbox windows-runtime

我尝试使用 ListBox 显示一组 Type 名称 在 XAML 中:

<ListBox x:Name="box"></ListBox>

在代码后面

box.ItemsSource= new List<Type>(){typeof(double), typeof(int)};

但是ListBox显示的是空字符串,虽然我能感觉到Items列表中确实有两个项目可以点击。

即使我将 ItemTemplate 更改为以下内容,Type 类的 FullName 属性也不会显示

<ListBox x:Name="box">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=FullName}"></TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这是怎么回事?以下显示名称,但不是我想要的。我想要存储 Type

box.ItemsSource= new List<string>(){typeof(double).FullName, typeof(int).FullName};

(我试过ComboBox,也有同样的问题)

最佳答案

这真是奇怪,根本没有绑定(bind)错误。我不知道为什么当你直接绑定(bind)到 Type 对象时会发生这种情况(也许它与 System 命名空间有关),但有一个解决方法是创建包装类:

public class TypeWrapper
{
    private Type _type;

    public TypeWrapper(Type type)
    {
        _type = type;
    }

    public Type Type { get { return _type; } }
}

然后按如下方式创建 ItemsSource:

box.ItemsSource = new List<TypeWrapper>()
            {
                new TypeWrapper(typeof (double)),
                new TypeWrapper(typeof (int))
            };

并且在您使用的 XAML 中

<TextBlock Text="{Binding Type.FullName}"></TextBlock>

代替

<TextBlock Text="{Binding FullName}"></TextBlock>

关于c# - WinRT 应用程序 : ComboxBox and ListBox does not display anything when ItemsSource is List<Type>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34823391/

相关文章:

c# - 数组如何实现 IList<T> 而不在 C# 中实现属性 "Count"?

c# - WPF:复杂图像的快速绘制/缩放

c# - 使用 win 表单组合框 Items.AddRange 方法

c# - WPF ComboBox XML 绑定(bind)和 ViewModel 绑定(bind)?

c# - 在一次操作中更改多个按钮的文本

c# - 使用 C# 和 WMI 计算的 CPU 使用率错误

c# - 是否有 C# 方法重载参数排序约定?

c# - 将 VisualStateManager VisualState 绑定(bind)到 View 模型?

c# - 无法将 TwoWay 模式设置为 x :Bind

c# - 在组合框中显示自定义值