How can the whitespace at the start and end of the content of an XML element be removed when binding to the element using XPath in WPF?
A:
Assuming you mean something like:
<x:XData>
<foo> Bar </foo>
</x:Data>
then there's no whitespace to trim here. Whitespace immediately following the opening tag, and immediately preceding the closing tag, is insignificant, and will be ignored anyway.
Pavel Minaev
2009-12-01 22:00:33
The problem is that the whitespace is rendered in the GUI using WPF.
Thomas Bratt
2009-12-01 22:02:39
Can you show 1) sample of XML you're binding to that exhibits the problem, and 2) the element that binds to it, and the binding itself?
Pavel Minaev
2009-12-01 22:04:38
Using the XML data from your example: DisplayMemberBinding="{Binding XPath=foo}"
Thomas Bratt
2009-12-01 22:07:40
Hm, you're right, for good or bad the whitespace is interpreted literally in the binding. Well, there's no XPath function to do precisely what you want - the closest is `normalize-space()`, but it will also replace each sequence of whitespace characters _within_ the string with a single space. If that's not good enough for you, you'll have to write your own `IValueConverter`, and apply that to the binding.
Pavel Minaev
2009-12-01 22:49:48
normalize-space() would be fine but I could not seem to get it to work when selecting an element.
Thomas Bratt
2009-12-02 07:49:26
Ahh... unfortunately, documentation for `Binding.XPath` property has this bit: "XPath functions are not supported". So value converter is the only way.
Pavel Minaev
2009-12-02 17:00:35