将列表框绑定到 XmlDocument

发布于 2024-08-22 17:03:42 字数 1081 浏览 9 评论 0原文

有人可以帮我解决为什么我的列表框是空的吗?

XmlDocument 包含以下 XML:

<Config>
  <Tabs>
    <Tab Name="Test1" />
    <Tab Name="Test2" />
  </Tabs>
</Config>

在我的 XAML 文件中,我尝试了以下操作,

<Window>
  <Grid>
     <ListBox DataContext="{Binding {StaticResource Data}, XPath=//Tabs}" ItemsSource="{Binding XPath=Tab/@Name}">
     </ListBox>
  </Grid>
<Window>

我知道我还没有设置与 name 属性的绑定,但如果它正常工作,不应该为每个选项卡节点显示 XmlDocument.XmlNode.ToString() ?

我的 C# 构造函数代码如下:

InitializeComponent();
this.doc = new XmlDocument();
doc.LoadXml(config.document.OuterXml);
XmlDataProvider provider = (XmlDataProvider)Resources["Data"];
provider.Document = doc;
provider.Refresh();

config.document.OuterXml 是包含上述 xml 的有效文档。

我使用 Collections 处理过程代码,但我一直在尝试找出如何直接绑定到 XML。

更新:ListBox 为空

现在没有绑定错误,但我的列表框为空,我仔细检查了我的 XML 文件,甚至执行了 MessageBox.Show(provider.Document.OuterXML) 并可以确认 XmlDocument 确实具有正确的内容节点。

提前致谢

Could someone help me out, to why my listbox is empty?

The XmlDocument contains the following XML:

<Config>
  <Tabs>
    <Tab Name="Test1" />
    <Tab Name="Test2" />
  </Tabs>
</Config>

In my XAML file I have tried the following

<Window>
  <Grid>
     <ListBox DataContext="{Binding {StaticResource Data}, XPath=//Tabs}" ItemsSource="{Binding XPath=Tab/@Name}">
     </ListBox>
  </Grid>
<Window>

I know I haven't set up the binding to name attribute but shouldn't this display XmlDocument.XmlNode.ToString() for each Tab Node if it was working?

My C# Constructor Code behind:

InitializeComponent();
this.doc = new XmlDocument();
doc.LoadXml(config.document.OuterXml);
XmlDataProvider provider = (XmlDataProvider)Resources["Data"];
provider.Document = doc;
provider.Refresh();

With config.document.OuterXml being a valid document containing the above xml.

I got this working with procedural code using Collections, but I have been trying to figure out how to bind directly to XML.

Update: ListBox empty

Now there is no binding errors, but my listbox is coming up empty, I have double checked my XML file, and even did MessageBox.Show(provider.Document.OuterXML) and can confirm that the XmlDocument does have the correct nodes.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一指流沙 2024-08-29 17:03:42

如果将 XmlDataProviderDocument 属性设置为 XmlDocument,则每当 XmlNode.NodeChanged 时,它都会刷新绑定。 code> 事件被引发。由于 Document 不是依赖属性,因此您无法绑定到它,因此您必须在代码中设置它;这应该可以解决问题:

在您的 XAML 中:

<Window.Resources>
   <XmlDataProvider x:Key="Data"/>
</Window.Resources>

...

<ListBox 
    DataContext="{Binding {StaticResource Data}, XPath=Config/Tabs}"
    ItemsSource="{Binding XPath=Tab/@Name}"/>

在窗口的构造函数中:

InitializeComponent();
XmlDocument d = new XmlDocument();
d.Load("MyData.xml");
XmlDataProvider p = (XmlDataProvider)Resources["Data"];
p.Document = d;

现在,您对 XmlDocument 所做的任何更改都将反映在 ListBox 中。

编辑:

我无法告诉你你做错了什么,但当你将你正在做的事情与下面的内容(这是一个完整的工作示例)进行比较时,也许你就能知道。

Window1.xaml:

<Window x:Class="Test.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1">
    <Window.Resources>
        <XmlDataProvider x:Key="Data"/>
    </Window.Resources>
    <ListBox 
        DataContext="{Binding Source={StaticResource Data}, XPath=Config}" 
        ItemsSource="{Binding XPath=Tabs/Tab/@Name}"/>     
</Window>

Window1.xaml.cs:

using System.Windows;
using System.Windows.Data;
using System.Xml;

namespace Test
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            XmlDocument d = new XmlDocument();
            string xml = @"<Config><Tabs><Tab Name='foo'/><Tab Name='bar'/></Tabs></Config>";
            d.LoadXml(xml);
            ((XmlDataProvider) Resources["Data"]).Document = d;
        }
    }
}

If you set the XmlDataProvider's Document property to your XmlDocument, it will refresh the bindings any time the XmlNode.NodeChanged event is raised. Since Document isn't a dependency property, you can't bind to it, so you have to set it in code; this should do the trick:

In your XAML:

<Window.Resources>
   <XmlDataProvider x:Key="Data"/>
</Window.Resources>

...

<ListBox 
    DataContext="{Binding {StaticResource Data}, XPath=Config/Tabs}"
    ItemsSource="{Binding XPath=Tab/@Name}"/>

In the window's constructor:

InitializeComponent();
XmlDocument d = new XmlDocument();
d.Load("MyData.xml");
XmlDataProvider p = (XmlDataProvider)Resources["Data"];
p.Document = d;

Now any changes you make to your XmlDocument will be reflected in the ListBox.

Edit:

I can't tell you what you're doing wrong, but perhaps you'll be able to when you compare what you're doing with the below, which is a complete working example.

Window1.xaml:

<Window x:Class="Test.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1">
    <Window.Resources>
        <XmlDataProvider x:Key="Data"/>
    </Window.Resources>
    <ListBox 
        DataContext="{Binding Source={StaticResource Data}, XPath=Config}" 
        ItemsSource="{Binding XPath=Tabs/Tab/@Name}"/>     
</Window>

Window1.xaml.cs:

using System.Windows;
using System.Windows.Data;
using System.Xml;

namespace Test
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            XmlDocument d = new XmlDocument();
            string xml = @"<Config><Tabs><Tab Name='foo'/><Tab Name='bar'/></Tabs></Config>";
            d.LoadXml(xml);
            ((XmlDataProvider) Resources["Data"]).Document = d;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文