使用 XML 作为项目源的 ListBox ItemTemplate 绑定中的问题
我遇到了一点麻烦...
这是我的“mappings.xml”文件...
<?xml version="1.0" encoding="utf-8"?>
<mappings>
<mapping QID="info1">
<empty></empty>
</mapping>
<mapping QID="info2">
<empty></empty>
</mapping>
</mappings>
我加载 XML 并将其设置为列表框的 ItemsSource 的方法:
(注意:我没有在 XAML 中使用
因为它提供一组 XML.XMLElement
,而不是我想要使用的 LINQ 兼容 XElement
,
Private Property myCollectionView as CollectionView
Private Property mappingsEnum as IEnumerable(Of System.Xml.Linq.XElement)
Sub LoadXML()
mappingsEnum = XDocument.Load("mappings.xml").Root.Elements
'using collection view so I can apply
'filtering to the list of <mapping> objects
myCollectionView = CollectionViewSource.GetDefaultView(mappingsEnum)
myListBox.ItemsSource = myCollectionView
End Sub
在 XAML 中
<Grid>
<ListBox x:Name="myListBox" ScrollViewer.VerticalScrollBarVisibility="Auto" DockPanel.Dock="Bottom" ScrollViewer.CanContentScroll="True" VirtualizingStackPanel.VirtualizationMode="Recycling" VirtualizingStackPanel.IsVirtualizing="True" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<!-- WHY DOESN'T THIS WORK?? -->
<TextBlock Text="{Binding XPath=@QID}">
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
我也尝试将 XPath 设置为“.@QID”,这没有什么区别,我检查了 myListBox.Items
集合并确认这些项目实际上是 XElement<。 /code> 对象看起来像
所以我认为那里没有问题
。最终结果是一个填充了空对象的列表框,我可以对它们进行过滤、排序,并向其中添加非绑定文本……但与源对象的任何绑定都不会呈现。
I'm having a bit o' trouble...
Here is my "mappings.xml" file...
<?xml version="1.0" encoding="utf-8"?>
<mappings>
<mapping QID="info1">
<empty></empty>
</mapping>
<mapping QID="info2">
<empty></empty>
</mapping>
</mappings>
My method that loads the XML and sets it as the ItemsSource for the listbox:
(Note: I didn't use <XMLDataProvider>
in XAML because it delivers a set of XML.XMLElement
, rather than the LINQ-Compatible XElement
I want to work with.
Private Property myCollectionView as CollectionView
Private Property mappingsEnum as IEnumerable(Of System.Xml.Linq.XElement)
Sub LoadXML()
mappingsEnum = XDocument.Load("mappings.xml").Root.Elements
'using collection view so I can apply
'filtering to the list of <mapping> objects
myCollectionView = CollectionViewSource.GetDefaultView(mappingsEnum)
myListBox.ItemsSource = myCollectionView
End Sub
in XAML
<Grid>
<ListBox x:Name="myListBox" ScrollViewer.VerticalScrollBarVisibility="Auto" DockPanel.Dock="Bottom" ScrollViewer.CanContentScroll="True" VirtualizingStackPanel.VirtualizationMode="Recycling" VirtualizingStackPanel.IsVirtualizing="True" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<!-- WHY DOESN'T THIS WORK?? -->
<TextBlock Text="{Binding XPath=@QID}">
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I've tried setting the XPath to ".@QID" as well, that made no difference. I inspected the myListBox.Items
collection and confirmed the items are in fact XElement
objects that look like <mapping QID="..."><empty/></mapping>
so I don't think there is an issue there.
The end result is a listBox that fills with empty objects. I can filter them, sort them, and add non-bound text to them... but any binding to the source object is left unrendered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://msdn.microsoft.com/ en-us/library/system.windows.data.binding.xpath.aspx
那里的一些引用:
以及如何绑定到 XElement
http://msdn.microsoft.com/ 的 一些示例en-us/library/cc165615.aspx
http://msdn.microsoft.com/en-us/library/system.windows.data.binding.xpath.aspx
Some quote from there:
And some example how to bind to XElement
http://msdn.microsoft.com/en-us/library/cc165615.aspx