从一个WPF列表框一个列表显示多种类型? [英] Display multiple types from a single list in a WPF ListBox?
问题描述
我是比较新的WPF中,它看起来像有可能是一堆方法可以做到这一点,但我不能得到任何我曾经想工作的人的。
我有一个包含两种不同类型的一个ObservableCollection。
我想这个列表绑定到一个列表框和遇到的每个类型显示不同的DataTemplates。我无法弄清楚如何根据类型自动切换数据模板。
我试图使用DataTemplate中的数据类型属性,并使用CONTROLTEMPLATES试图和DataTrigger,但无济于事,要么什么也不显示,或者声称无法找到我的类型...
实例下尝试:
$ ; b$ b
我只有连接到ListBox现在的一个数据模板,但即使不工作
XAML:
<窗口x:类=WpfApplication1.Window1
的xmlns =http://schemas.microsoft。 COM / WinFX的/ 2006 / XAML /演示
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=窗口1HEIGHT =300宽=300>
< Window.Resources>
<的DataTemplate X:键=PersonTemplate>
< TextBlock的文本={绑定路径=名称}>< / TextBlock的>
< / DataTemplate中>
<的DataTemplate X:键=QuantityTemplate>
< TextBlock的文本={绑定路径=金额}>< / TextBlock的>
< / DataTemplate中>
< /Window.Resources>
<网格和GT;
<&DockPanel中GT;
< ListBox的X:名称=MyListBoxWIDTH =250HEIGHT =250
的ItemsSource ={绑定路径= ListToBind}
的ItemTemplate ={StaticResource的PersonTemplate} >< /列表框>
< / DockPanel中>
< /网格和GT;
< /窗GT;
代码背后:
公共类Person
{
公共字符串名称{;组; }
公众人物(字符串名称)
{
名称=名称;
}
}
公共类产品数量
{
公众诠释金额{搞定;组; }
公共产品数量(INT金额)
{
金额=金额;
}
}
公共部分类窗口1:窗口
{
&的ObservableCollection LT;对象> ListToBind =新的ObservableCollection<对象>();
公共窗口1()
{
的InitializeComponent();
ListToBind.Add(新的Person(名称1));
ListToBind.Add(新的Person(名称2));
ListToBind.Add(新产品数量(123));
ListToBind.Add(新的Person(NAME3));
ListToBind.Add(新的Person(NAME4));
ListToBind.Add(新产品数量(456));
ListToBind.Add(新的Person(NAME5));
ListToBind.Add(新产品数量(789));
}
}
您说,它声称它无法找到我的类型。这是你应该解决问题。
这个问题,最有可能的是,你不创造引用您的CLR命名空间和装配的XAML命名空间声明。你需要把这样的事情在XAML的顶级元素:
的xmlns:富=CLR的命名空间:MyNamespaceName;装配= MyAssemblyName
一旦你这样做,XAML就知道与XML命名空间前缀任何富
其实就是 MyAssemblyName
在 MyNamespaceName
命名空间。
然后就可以引用XML命名空间中创建该的DataTemplate
标记:
<数据类型的DataTemplate ={富:PERSON}>
您当然可以建立一个模板选择,但是这增加了一块克鲁夫特到你的软件没有按'吨需要在那里。有在WPF应用程序模板选择的地方,但是这是不是。
I'm relatively new to WPF, and it looks like there are probably a bunch of ways to do this, but I can't get any of the ones I have tried to work.
I have an ObservableCollection that contains two different types.
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.
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...
Example Attempt below:
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>
Code Behind:
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.
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"
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.
Then you can reference that XML namespace in the markup that created the DataTemplate
:
<DataTemplate DataType="{foo:Person}">
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列表框一个列表显示多种类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!