从 WPF ListBox 中的单个列表显示多种类型? [英] Display multiple types from a single list in a WPF ListBox?
问题描述
我有一个包含两种不同类型的 ObservableCollection
.
I have an ObservableCollection<Object>
that contains two different types.
我想将此列表绑定到一个 ListBox 并为遇到的每种类型显示不同的 DataTemplates.我不知道如何根据类型自动切换数据模板.
I want to bind this list to a ListBox and display different DataTemplates for each type encountered. I can't figure out how to automatically switch the data templates based on the type.
我尝试使用 DataTemplate 的 DataType 属性并尝试使用 ControlTemplates 和 DataTrigger,但无济于事,要么没有显示,要么声称找不到我的类型...
I have attempted to use the DataType property of the DataTemplate and attempted using ControlTemplates and a DataTrigger, but to no avail, either it nothing shows up, or it claims it can't find my types...
下面的示例尝试:
我现在只有一个数据模板连接到 ListBox,但即使这样也行不通.
I only have the one data template wired to the ListBox right now, but even that doesn't work.
XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="PersonTemplate">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</DataTemplate>
<DataTemplate x:Key="QuantityTemplate">
<TextBlock Text="{Binding Path=Amount}"></TextBlock>
</DataTemplate>
</Window.Resources>
<Grid>
<DockPanel>
<ListBox x:Name="MyListBox" Width="250" Height="250"
ItemsSource="{Binding Path=ListToBind}"
ItemTemplate="{StaticResource PersonTemplate}"></ListBox>
</DockPanel>
</Grid>
</Window>
背后的代码:
public class Person
{
public string Name { get; set; }
public Person(string name)
{
Name = name;
}
}
public class Quantity
{
public int Amount { get; set; }
public Quantity(int amount)
{
Amount = amount;
}
}
public partial class Window1 : Window
{
ObservableCollection<object> ListToBind = new ObservableCollection<object>();
public Window1()
{
InitializeComponent();
ListToBind.Add(new Person("Name1"));
ListToBind.Add(new Person("Name2"));
ListToBind.Add(new Quantity(123));
ListToBind.Add(new Person("Name3"));
ListToBind.Add(new Person("Name4"));
ListToBind.Add(new Quantity(456));
ListToBind.Add(new Person("Name5"));
ListToBind.Add(new Quantity(789));
}
}
推荐答案
您说它声称无法找到我的类型."这是你应该解决的问题.
You say that "it claims it can't find my types." That's a problem that you should fix.
问题很可能是您没有在 XAML 中创建引用您的 CLR 命名空间和程序集的命名空间声明.您需要在 XAML 的顶级元素中放入这样的内容:
The problem, most likely, is that you're not creating a namespace declaration in the XAML that references your CLR namespace and assembly. You need to put something like this in the XAML's top-level element:
xmlns:foo="clr-namespace:MyNamespaceName;assembly=MyAssemblyName"
一旦你这样做,XAML 就会知道任何带有 XML 命名空间前缀 foo
的东西实际上是 MyNamespaceName
命名空间中 MyAssemblyName
中的一个类.
Once you do this, XAML will know that anything with the XML namespace prefix foo
is actually a class in MyAssemblyName
in the MyNamespaceName
namespace.
然后您可以在创建 DataTemplate
的标记中引用该 XML 命名空间:
Then you can reference that XML namespace in the markup that created the DataTemplate
:
<DataTemplate DataType="{foo:Person}">
您当然可以构建模板选择器,但这会为您的软件添加一些不需要的东西.WPF 应用程序中有模板选择器的位置,但不是这样.
You can certainly build a template selector, but that's adding a piece of cruft to your software that doesn't need to be there. There's a place for template selectors in WPF applications, but this isn't it.
这篇关于从 WPF ListBox 中的单个列表显示多种类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!