views:

264

answers:

1

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
The problem is that the whitespace is rendered in the GUI using WPF.
Thomas Bratt
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
Using the XML data from your example: DisplayMemberBinding="{Binding XPath=foo}"
Thomas Bratt
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
normalize-space() would be fine but I could not seem to get it to work when selecting an element.
Thomas Bratt
Ahh... unfortunately, documentation for `Binding.XPath` property has this bit: "XPath functions are not supported". So value converter is the only way.
Pavel Minaev