gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:27:16 24 4
gpt4 key购买 nike

我尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com