New to Telerik UI for WPF? Download free 30-day trial

Formatting Display of DateTime Values in RadComboBox

Environment

Product Version 2023.2.718
Product RadComboBox for WPF

Description

How to format DateTime values to strings in the Selection Box element of the RadComboBox.

Solution

To get the desired effect, you can bind to the SelectedItems or to the original string value and use an IValueConverter to format the values.

public class DateTimeStringConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        var dateStrings = ((string)value).Split(','); 
        var format = parameter.ToString(); 
        List<string> result = new List<string>(); 
        foreach (string dateString in dateStrings) 
        { 
            var date = DateTime.Parse(dateString, CultureInfo.InvariantCulture); 
            result.Add(date.ToString(format, CultureInfo.InvariantCulture)); 
        } 
        return string.Join(", ", result); 
    } 
} 
In the XAML code you can use the RadComboBox MultipleSelectionBoxTemplate property and bind the Text property of the TextBlock to the context provided in the DataTemplate with the converter.

<telerik:RadComboBox x:Name="radComboBox" AllowMultipleSelection="True"> 
    <telerik:RadComboBox.MultipleSelectionBoxTemplate> 
        <DataTemplate> 
            <TextBlock Text="{Binding Converter={StaticResource DateTimeStringConverter}, ConverterParameter='m/dd/yyyy' }"/> 
        </DataTemplate> 
    </telerik:RadComboBox.MultipleSelectionBoxTemplate> 
</telerik:RadComboBox> 
In this article