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