Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringFormat for binding TimeSpan in XAML does not work

Tags:

c#

wpf

xaml

I have a TimeSpan variable called Time which I managed to successfully bind in XAML. Here's the code that I have:

...
<TextBlock Grid.Column="2" Text="{Binding Time, StringFormat='{}{0:hh:mm}'}" />
...

The values are now displayed properly, however, the format is still hh:mm:ss. I'm trying to get rid of the seconds and display only the hours and the minutes but for some reason the changes that I make to the StringFormat are not accounted for and I always get the same hh:mm:ss format. Can you please advise?

Thank you!

EDIT:

Judging by the comments below it seems that the Time that I have binded is not in TimeSpan format as the StringFormat seems to be correct but the displayed value is wrong. The Time variable is defined as follows in a custom class Record:

        private TimeSpan time;
        public TimeSpan Time
        {
            get { return time; }
            set { time = value; }
        }

In my code I have an observable collection defined:

ObservableCollection<Record> records = new ObservableCollection<Record>();

The collection is the populated an then I have:

listBoxRecords.ItemsSource = this.records;

Here's the full XAML, keeping in mind that the rest of the fields are correctly populated, only the TimeSpan is giving me troubles:

<ListBox Margin="0,44,0,58" Name="listBoxRecords" ItemsSource="{Binding records}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="4">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="120" />
                            <ColumnDefinition Width="85" />
                            <ColumnDefinition Width="55" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding Date, StringFormat='{}{0:dd-MM-yyyy HH:mm}'}" />
                        <TextBlock Grid.Column="1" Text="{Binding Type }" />
                        <TextBlock Grid.Column="2" Text="{Binding Time, StringFormat='{}{0:hh\\:mm}'}" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
like image 984
mmvsbg Avatar asked Nov 25 '13 11:11

mmvsbg


5 Answers

You can get that output by using this StringFormat:

<TextBlock Text="{Binding Time, StringFormat={}{0:hh}:{0:mm}, FallbackValue=00:00}" />

UPDATE >>>

The first point that I'd like to make is that hh will display hours in a 12 hour format and you should use HH if you want 24 hour format.

The second point that I'd like to make is that you are clearly confused as to what you are doing in your application. The time value that you are seeing with the seconds value is not coming from this TextBlock and probably not coming from your one either. I think that you need to take a good look at your code... you must have another TextBlock or something.

like image 165
Sheridan Avatar answered Nov 15 '22 06:11

Sheridan


Try escaping : by changing your StringFormat to something like:

StringFormat='{}{0:hh\\:mm}'
like image 32
dkozl Avatar answered Nov 15 '22 06:11

dkozl


None of the proposed answers worked for me, they all produces binding errors. I ended up making a sample app to test every possible string format found on MSDN: Custom TimeSpan Format Strings

I found from this testing that only the TimeSpan parts which start with % (like %d for Days) worked in WPF StringFormat property.

Try this, it worked for me:

<TextBlock
    Text="{Binding Path=Time,
        StringFormat={}{0:%d} days {0:%h} hours {0:%m} minutes,
        FallbackValue=0 days 0 hours 0 minutes}" />
like image 22
turkinator Avatar answered Nov 15 '22 05:11

turkinator


from .Net 4 for timespan you can use

    <TextBlock Grid.Column="2" Text="{Binding Time, StringFormat=hh\\:mm\\:ss}" />

as for tim-lloyd answer in How to format TimeSpan in XAML

Mark as answer if it worked.

Regards

like image 36
Maurizio momix Verde Avatar answered Nov 15 '22 05:11

Maurizio momix Verde


For those interested in a programmatic approach, this worked for me:

        {
            DataGridTextColumn dgtc = new DataGridTextColumn();
            dgtc.Header = "End Time";
            dgtc.Binding = new Binding("EndTime");
            dgtc.Binding.StringFormat = "{0:hh}:{0:mm}";
            ScheduleGrid.Columns.Add(dgtc);
        }
like image 41
BitsAndBytes Avatar answered Nov 15 '22 04:11

BitsAndBytes