One option is to go for a converter, which formats the text of items:
<ComboBox ItemSource="{Binding LogTimeSpan}"
Grid.Row="15"
Grid.Column="1"
Grid.ColumnSpan="2">
<ComboBox.Resources>
<ns:TimeSpanConverter x:Key="TimeSpanConverter" />
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=., Mode=OneTime, Converter={StaticResource TimeSpanConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Where you can implement your converter like the following
using System;
using System.Globalization;
using System.Windows.Data;
public class TimeSpanConverter : IValueConverter
{
public object Convert(object value,
Type targetType,
object parameter,
CultureInfo culture)
{
// I am using following extension method here: http://stackoverflow.com/a/4423615/57508
var timeSpan = (TimeSpan) value;
var result = timeSpan.ToReadableString();
return result;
}
public object ConvertBack(object value,
Type targetType,
object parameter,
CultureInfo culture)
{
throw new NotImplementedException();
}
}
If you just need a basic format, you can also use the StringFormat
of a binding:
<ComboBox ItemSource="{Binding LogTimeSpan}"
Grid.Row="15"
Grid.Column="1"
Grid.ColumnSpan="2">
<ComboBox.Resources>
<ns:TimeSpanConverter x:Key="TimeSpanConverter" />
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=., Mode=OneTime, StringFormat={}{0:hh\\:mm\\:ss}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>