0

I have the following binding in my xaml, I can see the double value shown however the StringFormat is completely ignored.

<Label Content="{Binding ByteCount, StringFormat=n}"/>

ByteCount property is of type double. I even changed it even to string and it still doesn't work.

What could be the reason please?

Update:

public double ByteCount
{
            get
            {
                return CloneHelper.GetSize(this);
            }
}

public static class CloneHelper
    {
        public static double GetSize(BookSetViewModel book)
        {
            .....

            return total;
        }
}
9
  • Perhaps you can include the actual binding in your question? (btw: a ByteCount property represented as a double!?) Commented Oct 31, 2011 at 12:54
  • If ByteCount is an int, you might be missing a converter. Commented Oct 31, 2011 at 12:55
  • 1
    try removing Mode=TwoWay first, don't see any sense of it with Label Commented Oct 31, 2011 at 12:58
  • Does the output window in Visual Studio report any BindingExpression failures? Commented Oct 31, 2011 at 13:00
  • I have added the requested binding. Many Thanks Commented Oct 31, 2011 at 13:03

3 Answers 3

6

WPF's Label actually has a ContentStringFormat property which overwrites whatever the binding's StringFormat is

Set ContentStringFormat instead

<Label Content="{Binding ByteCount}" ContentStringFormat="n" />
Sign up to request clarification or add additional context in comments.

2 Comments

You are a star Rachel. Amazing found, this was it!!! :) Is this the same for silverlight? I need to test this...
@Kave I'm not sure if it's the same for Silverlight, but I would assume so. Should be easy to write a quick test and find out.
0

The StringFormat property has peculiar syntax which you have to use.

For your example, you should use this snippet.

<Label Content="{Binding ByteCount, StringFormat={}{0:n}}" />

Here's a link to the MSDN page for the StringFormat property.

2 Comments

It works perfectly well without, the {} is just there to escape the curly braces. "StringFormat=n" should work (and works here) too.
I think you miss a } at the end before ". Nonetheless it doesn't work either. I don't understand...
0

Have no idea what is the reason, maybe because of target type (for label it is object), but it works ok for the TextBlock:

<TextBlock Text="{Binding ByteCount, StringFormat=n}"/>

Comments

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.