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>
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.
Try escaping :
by changing your StringFormat
to something like:
StringFormat='{}{0:hh\\:mm}'
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}" />
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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With