问题描述
我正在编写一个WPF接口,读取XML文件并显示数据。 XML文件将是这样的:
< People>
< Person Gender =Male>
< Name> Joe< / Name>
< / Person>
< Person Gender =Female>
< Name> Susan< / Name>
< / Person>
< / People>
我需要一个取决于Person的Gender属性的数据模板。此数据模板用于列表框项目。列表框的来源是People。
< DataTemplate DataType =Person [@ Gender ='Male']>
< / DataTemplate>
< DataTemplate DataType =Person [@ Gender ='Female']>
< / DataTemplate>
我以为这会像上面的代码一样,但不行。任何想法?
谢谢!
ANSWER
以下是上述XML示例的DataTemplateSelector:
public class MyDataTemplateSelector:DataTemplateSelector
{
private DataTemplate _MaleTemplate = null;
private DataTemplate _FemaleTemplate = null;
public DataTemplate MaleTemplate
{
get {return _MaleTemplate; }
set {_MaleTemplate = value; }
}
public DataTemplate FemaleTemplate
{
get {return _FemaleTemplate; }
set {_FemaleTemplate = value;
}
public override DataTemplate SelectTemplate(object item,DependencyObject container)
{
XmlElement currentNode =(XmlElement)item;
DataTemplate selectedTemplate = null;
string selectedGender =;
selectedGender = currentNode.GetAttribute(Gender);
开关(selectedGender)
{
caseMale:
selectedTemplate = _MaleTemplate;
break;
case女:
selectedTemplate = _FemaleTemplate;
break;
默认值:
break;
}
return selectedTemplate;
}
}
感谢您的帮助!我希望这对其他人也是有用的!
您需要使用 DataTemplateSelector 。 WPF博士的表明了这一点完全是你正在寻找的。 p>
I'm working on a WPF interface that reads an XML file and displays the data. The XML file would be something like this:
<People>
<Person Gender="Male">
<Name>Joe</Name>
</Person>
<Person Gender="Female">
<Name>Susan</Name>
</Person>
</People>
I need a data template dependent on the Gender attribute of Person. This data template is for list box items. the source of the listbox is People.
<DataTemplate DataType="Person[@Gender='Male']">
</DataTemplate>
<DataTemplate DataType="Person[@Gender='Female']">
</DataTemplate>
I thought it would be something like the above line of code, but it doesn't work. Any ideas?
Thanks!
ANSWER
Here's the DataTemplateSelector for the above XML example:
public class MyDataTemplateSelector : DataTemplateSelector
{
private DataTemplate _MaleTemplate = null;
private DataTemplate _FemaleTemplate = null;
public DataTemplate MaleTemplate
{
get { return _MaleTemplate; }
set { _MaleTemplate = value; }
}
public DataTemplate FemaleTemplate
{
get { return _FemaleTemplate; }
set { _FemaleTemplate = value; }
}
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
XmlElement currentNode = (XmlElement)item;
DataTemplate selectedTemplate = null;
string selectedGender = "";
selectedGender = currentNode.GetAttribute("Gender");
switch (selectedGender)
{
case "Male":
selectedTemplate = _MaleTemplate;
break;
case "Female":
selectedTemplate = _FemaleTemplate;
break;
default:
break;
}
return selectedTemplate;
}
}
Thanks for the help! I hope this will be useful to someone else as well!
You need to use a DataTemplateSelector. This article by Dr. WPF demonstrates it completely and is just what you are looking for.
这篇关于如何根据XML属性创建数据模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!