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
If you set the
XmlDataProvider‘sDocumentproperty to yourXmlDocument, it will refresh the bindings any time theXmlNode.NodeChangedevent is raised. SinceDocumentisn’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
XmlDocumentwill 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: