2

In my current project i'm trying to show a label which has a TimeSpan binding. Actually the xaml looks simple:

<Label Content="{Binding ElementName=Root, Path=DataSource.TimeTotal, StringFormat={}{0:hh\:mm}}"/>

Which is exactly what is shown in this question. But it doesn't work, it shows always the full time and not only hours + minutes.

I tested a lot of solutions but none of them worked.

Community
  • 1
  • 1
Felix K.
  • 6,201
  • 2
  • 38
  • 71
  • What version of .NET are you using? TimeSpan only gained format strings in .NET 4. – Jon Skeet May 14 '12 at 18:52
  • @Asif Tested it already, doesn't work. – Felix K. May 14 '12 at 18:59
  • @JonSkeet .NET 4.0, non-client profile. That's the point its not working and it makes me angry right now. :-/ – Felix K. May 14 '12 at 18:59
  • 1
    Maybe just try to use `TextBox` instead of a `Label` and bind `Text` instead of `Content`. You can find example of the binding here: http://stackoverflow.com/questions/4563081/how-to-format-timespan-in-xaml. Let me know if it works for you. – Lukasz M May 14 '12 at 19:23
  • @Lucas Actually this is working, but i'm wondering why it's not supported by .NET so i gonna leave this question open for other readers. – Felix K. May 14 '12 at 19:42
  • 1
    It works with TextBlock, which is the lightweight equivalent to Label. Whenever you're binding to a dependency property in a control that uses a `ContentPresenter` to display the data, it seems to eat the `StringFormat` you've provided. (in `Label`, `Button`, etc). Wish someone would let us know why! You can nest a `TextBlock` within the `Label` or just go with a `TextBlock` (which, unless you need access text, is usually a better choice anyway). – erodewald May 14 '12 at 20:07
  • Since it worked, I'll post it as an answer. I'll also add some more information that I've just found :). – Lukasz M May 14 '12 at 20:25
  • @Felix: I meant posting my comment with using `TextBlock` instead of `Label` and extending it with some alternative solutions and other information I've found. – Lukasz M May 14 '12 at 20:52
  • @FelixK. Sure, here it is [as an answer](http://stackoverflow.com/a/10591149/24399). – erodewald May 14 '12 at 21:14

2 Answers2

4

Suggested solution

Maybe just try to use TextBox instead of a Label and bind Text instead of Content. You can find example of the binding here: How to format TimeSpan in XAML. It's also worth noticing that TextBlock is more lightweight then Label, so it should be used when possible.

Alternative solutions

If you want to use Label for some reason, there seems to be a property called ContentStringFormat of a Label itself, so your xaml can look like:

<Label Content="{Binding ElementName=Root, Path=DataSource.TimeTotal}" ContentStringFormat="hh\\:mm"/>

Another solution would be implementing a custom IValueConverter, where you can pass string format as converter's parameter and format the value manually in the converter's code. This solution should then work correctly for all appropriate control types. More about value converters can be found here and here.

String formatting properties

As Erode wrote in the comment, there seem to be other controls for which StringFormat does not work. However, for controls derived from ContentControl you should be able to use ContentStringFormat and for controls derived from ItemsControl, there is a ItemStringFormat property. There is a link with few samples presenting how to use those: http://blogs.msdn.com/b/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx

Community
  • 1
  • 1
Lukasz M
  • 5,635
  • 2
  • 22
  • 29
2

It works with TextBlock, which is the lightweight equivalent to Label. Whenever you're binding to a dependency property in a control that uses a ContentPresenter to display the data, it seems to eat the StringFormat you've provided. (in Label, Button, etc). Wish someone would let us know why! You can nest a TextBlock within the Label or just go with a TextBlock (which, unless you need access text, is usually a better choice anyway).

erodewald
  • 1,815
  • 20
  • 45