【发布时间】:2017-03-04 20:37:43
【问题描述】:
我创建了一个自定义 DataSourceProvider,它提供了一个 List<XmlNode> 作为查询结果。
通过使用 Path 参数,生成的 Listbox 应该包含在 XmlNode 中命名的属性的指定列表
Path=Tag 应使用列表中包含的标记填充列表框。但它使用第一个 List-items 标记填充列表。
问题是为什么 Path 参数不检索 List 中的所有 Tag 元素,而只检索第一个列表元素 Tag 中的字母。
自定义列表作为资源提供:
<Window.Resources>
<classes:Custom_XmlProvider x:Key="txtProvider" Path="name" />
</Window.Resources>
ListBox 使用 Path=Tag 绑定到资源:
<ListBox Grid.Row="1" ItemsSource="{Binding Path=Tag, Source={ StaticResource txtProvider}}"
DockPanel.Dock="Bottom">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding}" FontFamily="Arial" FontWeight="Normal" Margin="5,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
自定义 DataSourceProvider 返回包含 1200 个对象的 List:
protected override void BeginQuery()
{
System.Threading.ThreadPool.QueueUserWorkItem(RunQuery, null);
}
private void RunQuery(object state)
{
base.BeginQuery();
try
{
//TODO - do something with the NodeList
c_pData = c_thisXmlParser.NodeList;
OnQueryFinished(c_pData, null, null, c_pData);
}
catch
{
OnQueryFinished(null, null, null, null);
}
}
XmlNode 定义为:
public class XmlNode
{
public string Tag { get; set; }
public string Type { get; set; }
public string InnerText { get; set; }
public int FileNumber { get; set; }
public int NodeDepth { get; set; }
public int IndexNumber { get; set; }
}
这是列表中的第一个元素:
结果列表在窗口中显示如下:
上面的列表应该包含一个 Tag 元素的列表,我无法正确理解为什么 Path 参数没有收集 List 的正确元素,而是提供了第一个 list-elements tag-name 的列表.
【问题讨论】:
标签: c# wpf listbox dataprovider