Navigation

Search

Categories

On this page

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 120
This Year: 1
This Month: 0
This Week: 0
Comments: 40

Sign In
Pick a theme:

# Tuesday, October 06, 2009
Tuesday, October 06, 2009 9:46:36 AM (Mountain Daylight Time, UTC-06:00) ( All things Microsoft | Code )
I've resolved to start making the shift to WPF. It's about time. The technology has only been around for a good 3+ years. So far, I'm very excited by what I see. An invaluable reference has been Programming WPF 2nd edition by Chris Sells. Specifically the appendices. The explination of Xaml, and the Xaml Extensions removes the veil of confusion from all of the arcane looking symbols going on in the Xaml code. Thanks for the awesome book Chris! I was going through an article published in MSDN Magazine, Data Binding in WPF by John Papa. I encountered a very frustrating bug and I felt the need to let the next developer know about the solution I uncovered. (The next developer is sometimes me. I do very much love finding my own posts months or years down the road that help me out). Maybe this will help you out.

So, I'm building my Xaml out, and I have a perfectly good XmlDataProvider in my Resources.
<XmlDataProvider x:Key="MoreColors" XPath="/colors">
<x:XData>
<colors>
<color name="pink"/>
<color name="white"/>
<color name="black"/>
<color name="cyan"/>
<color name="gray"/>
<color name="magenta"/>
</colors>
</x:XData>
</XmlDataProvider>

I Created the Binding to my Listbox as such.


This whole thing is working in the Designer (Visual Studio 2008), but is displaying nothing at runtime. I whisk away to the MSDN Documentation for XmlDataProvider and find this clever little note.

The root node of the XML data has an xmlns attribute that sets the XML namespace to an empty string. This is a requirement for applying XPath queries to a data island that is inline within the XAML page. In this inline case, the XAML, and thus the data island, inherits the System.Windows namespace. Because of this, you need to set the namespace blank to keep XPath queries from being qualified by the System.Windows namespace, which would misdirect the queries.
Is it possible that this is the explination to the Debug output I'm seeing in Visual Studio at runtime?

System.Windows.Data Error: 47 : XmlDataProvider has inline XML that does not explicitly set its XmlNamespace (xmlns="").

I add the missing xmlns="" to the root xml element (as shown below), and things begin to work at runtime.

<XmlDataProvider x:Key="MoreColors" XPath="/colors">
<x:XData>
<colors xmlns="">
<color name="pink"/>
<color name="white"/>

In searching for the answer to this solution, I also came across this post Why WPF databinding is an awful technology. I hope this is not shared feeling of frustration many of us receive by wasted hours of our day with the Moteur de recherche du jour (search engine of the day). It's a really useful tool if you can convince it to give you the right answer, and people have posted about the particular phrase you're looking for.

Good luck reader, with your journey to WPF enlightenment. It has been worth my time.

Comments are closed.