PropertyGrid, FormatString and TimeSpan?

Sep 13, 2011 at 1:03 AM


Thanks for a handy library, and particularly for making it available via NuGet!

I was wondering if it's at all possible to format that string for a TimeSpan?

I can't seem to get it to work, so I'm guessing the answer is no, but thought I'd ask in case I'm missing something.

Sep 13, 2011 at 7:36 PM

I just added some code that use the format strings when getting the values (in the PropertyViewModel class).

This should now work for both DateTime and TimeSpan properties.

The TimeSpanFormatter supports extra formatting codes: "MM" for total minutes, "SS" for total seconds etc. See the unit tests for examples.

The TimeSpanParser allows you to enter time spans as "5d" or "12m 3s".

I left one "todo" in the TimeSpanParser code, as the parser should also consider the FormatString. Hopefully someone knows how to solve that...

An upgrade of the NuGet package will come soon.

Sep 15, 2011 at 7:15 AM

Thanks I don't follow exactly what you mean, but I'll download the code and take a look when I get a chance.

I guess my goal would be to be able to use the FormatString attribute and essentially have it used as a StringFormat for the binding on the resulting TextBox.

I'm already using the following string in other bindings in my project.

<System:String x:Key="DurationFormatString">( {0:mm\:ss\.ff} )</System:String>

Feb 8, 2012 at 12:23 AM

Just a follow up for anyone else that may come across this post to say that the latest version at least supports exactly what I wanted.

You can add a FormatString attribute to your property and it will render as expected.

For example, here is the code for my property:

[Browsable( true )]
Description( "Depending on the element type acts as a duration/delay." )]
public TimeSpan Duration
get { return _duration; }
_duration = value;
InvokePropertyChanged( "Duration" );

Feb 11, 2012 at 11:45 AM

Great! :)

How is parsing working? Is it ok? Did you try entering "4m" (4 minutes) or "4m 20s" (4 minutes and 20 seconds)? Or "4:20"? Hope this works (I don't have time to test right now). If not we should add an issue on this.

Feb 27, 2012 at 11:19 PM

Yeah sorry I spoke too soon sorry.

I'll see about debugging further when I can, add an issue, etc

But basically it displays fine but when I change the value the bound value isn't being updated for whatever reason.

I did run the Unit Tests I thought were relevant but I guess I've missed something or it doesn't cover everything needed.

Feb 28, 2012 at 2:18 AM

A follow up.  I tried just using a TextBlock with StringFormat in the binding instead and noticed similar issues.

It turns out TimeSpan.Parse can't handle that string either, so things are being handled better than that anyway.

I tried to use another FormatString:

[FormatString( @"{0:m\m\ s\.fff\s}" )]

But that didn't work out.  So I've given up for now.  :)


Just to be clear an example string that PropertyEditor and TimeSpan.Parse can't handle is "04:20.0"  If you use "04:20" it will get parsed as hh:mm.

For now in my application I'm just going to remove the formatting and the users will have to deal with it appearing different in the edit box to the rendered text.

Mar 7, 2012 at 5:44 PM

I added a new class that uses the format string to parse TimeSpan strings (it converts the format string to a regular expression that is used for the parsing). See Helpers/FormattedTimeSpanParser.

The PropertyControl now uses the Converters/TimeSpanToStringConverter when the property is a TimeSpan, this seems to work for my test cases.