Display multiple types from a single list in a WPF ListBox?

允我心安 提交于 2019-11-29 02:01:15

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.

You have to use a DataTemplateSelector. See here for an example.

Addiontal information on MSDN

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!