使用 XML 作为项目源的 ListBox ItemTemplate 绑定中的问题

发布于 2024-09-06 11:04:16 字数 2036 浏览 1 评论 0原文

我遇到了一点麻烦...

这是我的“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裂开嘴轻声笑有多痛 2024-09-13 11:04:16

http://msdn.microsoft.com/ en-us/library/system.windows.data.binding.xpath.aspx

那里的一些引用:

LINQ to XML 对象 XDocument 和
XElement 不使用 XPath。为了
详细信息,请参阅如何:绑定到
XDocument、XElement 或 LINQ for XML
查询结果。

以及如何绑定到 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:

LINQ to XML objects XDocument and
XElement do not use XPath. For
details, see How to: Bind to
XDocument, XElement, or LINQ for XML
Query Results.

And some example how to bind to XElement

http://msdn.microsoft.com/en-us/library/cc165615.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文