And the Hotfix solved the problem! So if you plan to use TwoWay binding between XElement objects and a ComboBox or ListBox get the Hotfix from Microsoft. According to the description the Hotfix should be included in a future software update. But in which update this fix will be included is not written there. So check the version numbers of the files listed in the description to see if you still need the Hotfix.
Note: According to the description installing the hotfix should not require a restart of the computer. But on the two Systems where we installed the Hotfix (one 32bit and one 64bit system) we had to restart both systems.
For a dynamically built User Interface I’ve bound some controls to a XDocument tree that contained the data and the description of the types. The first controls (TextBox and a numeric Control) worked perfectly, so I was confident that the next controls would just be a routine work. So the next control I wanted to use was the ComboBox.
The data was shown as expected, but after changing the selection the new data was not saved. I’ve tried several changes in the binding (adding explicit two-way binding, setting the UpdateSourceTrigger to PropertyChanged) but the data was not updated when the selection changed. I’ve asked some guys in the office with more WPF experience, but they also saw no reason why the binding didn’t work. Even adding a Converter to check if the data types were not compatible gave me no clue. I’ve also asked on Stack Overflow but got no solution for my problem.
I proudly present to you the sixth in a infinite number of posts of “Slippery When Wet.” In these posts I show you a little bastard I stumbled on.
Recently, I tried to work on a XmlDocument with XPath in C#. To get the right expressions easily I used the XML Spy from Altova. As soon as I got the right XPath expressions I started Visual Studio to work in my .NET Application. But here, the only expression that worked was „/*“. According to the .NET Documentation the expressions should work, but they didn’t.
So I tried another XML file, one from the examples in the .NET Documentation of XPath. And with this file the (corresponding) expressions worked as expected. So I searched the difference of the two XML files. After a short time I found the relevant difference: The file that worked had no namespace defined, whereas the non-working file had two namespaces. One namespace with a prefix (xmlns:prefix=“URI“) and a default namespaces (xmlns=“URI“).
As I got the reason for my problem I had to look for a solution. A little bit of googling brought me the solution. I had to add the default namespace to the NamespaceManager with a prefix and then use the XPath expressions with the defined prefix.
In .NET 3.5 Microsoft introduced the XDocument class as a alternative (or a replacement) for XmlDocument. The C# code differs a little bit depending on the used class. I found a description when using the XmlDocument class and two when using the XDocumentclass.
With the added prefix for the default namespace my XPath expressions worked as expected.