1

This is probably pretty simple but I'm trying to take the value from an element in XML and put it in my DataGridTextColumn formatted to 3 decimal places but I can't get it to work...it just leaves it unformatted at like 20. Here is the line I am using. What am I doing wrong?

<DataGridTextColumn Header="Avg" Binding="{Binding Path=Element[avg].Value, StringFormat=0:0.###}"></DataGridTextColumn>

1 Answer 1

3

You need to change your string format specification a bit. This should work:

<DataGridTextColumn Header="Avg" 
    Binding="{Binding Path=Element[avg].Value, StringFormat={}{0:0.###}}">

Note that {} is required at the beginning, since { causes the first part of the StringFormat specifier to be treated as a markup extension.

Sign up to request clarification or add additional context in comments.

6 Comments

Actually, the exact same thing happens with this solution...it remains unformatted.
@novacara: What type is it being treated as? Is it an int value or a string? If so, the format string won't work, and you'd need to use an IValueConverter to make it a double or decimal value.
It is probably being treated as a string. Is there a way to specify the datatype in the xml or is my only option to make a class that implements IValueConverter?
@novacara: Value is always a string: msdn.microsoft.com/en-us/library/system.xml.xmlnode.value.aspx You'll need to either turn this into something more meaningful, or use an IValueConverter to change this to a double. The latter is probably nicest, as you could reuse it anywhere...
Alright, thanks for the nudge in the right direction. I am new to both XML and WPF.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.