将列表框绑定到 XmlDocument
有人可以帮我解决为什么我的列表框是空的吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果将
XmlDataProvider
的Document
属性设置为XmlDocument
,则每当XmlNode.NodeChanged
时,它都会刷新绑定。 code> 事件被引发。由于Document
不是依赖属性,因此您无法绑定到它,因此您必须在代码中设置它;这应该可以解决问题:在您的 XAML 中:
在窗口的构造函数中:
现在,您对
XmlDocument
所做的任何更改都将反映在ListBox
中。编辑:
我无法告诉你你做错了什么,但当你将你正在做的事情与下面的内容(这是一个完整的工作示例)进行比较时,也许你就能知道。
Window1.xaml:
Window1.xaml.cs:
If you set the
XmlDataProvider
'sDocument
property to yourXmlDocument
, it will refresh the bindings any time theXmlNode.NodeChanged
event is raised. SinceDocument
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:
In the window's constructor:
Now any changes you make to your
XmlDocument
will be reflected in theListBox
.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:
Window1.xaml.cs: