Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to bind a control to an element of a XmlDocument in the ViewModel (MVVM Pattern)?
    primarykey
    data
    text
    <p>I'm trying to bind a <strong>TextBlock</strong> to an element of a XML document, it works fine if the source is a StaticResouce, but I cannot figure out the syntax to bind to the same XML document when exposed through ViewModel. </p> <p>Here's the XML document:</p> <pre><code>&lt;Books&gt; &lt;Book ID="1" Name="ABC" /&gt; &lt;Book ID="2" Name="DEF" /&gt; &lt;Book ID="3" Name="XYZ" /&gt; &lt;/Books&gt; </code></pre> <p><strong>Binding to a StaticResource = SUCCESSFUL</strong></p> <p>The resource file contains a XmlDataProvider with reference to Books.xml:</p> <pre><code>&lt;XmlDataProvider x:Key="data" Source="Books.xml"/&gt; </code></pre> <p>And here's how I'm binding to the xml file.</p> <pre><code>&lt;TextBlock Text="{Binding Source={StaticResource data},XPath=/Books/Book/@Name}"/&gt; &lt;XmlDataProvider x:Key="data" Source="../Views/Data.xml" /&gt; </code></pre> <p>As I said before, the binding above works fine and it displays "ABC". However, that XmlDocument is supposed to come from SQL Server and exposed through ViewModel (MVVM Pattern). </p> <p><strong>Binding to ViewModel = FAILED</strong></p> <p>The Books property from the ViewModel is populated with the exactly same XML (see above):</p> <pre><code>public XmlDocument Books { get; set; } </code></pre> <p>And here's the syntax that I'm using to bind to ViewModel:</p> <pre><code>&lt;TextBlock Text="{Binding Books,XPath=/Books/Book/@Name}" /&gt; </code></pre> <p>It doesn't return anything and I'm getting the following error message:</p> <blockquote> <p>BindingExpression with XPath cannot bind to non-XML object.</p> </blockquote> <p>Any help is greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload