Search code examples
silverlight-4.0telerik-griddataformat

Only first letter by DataFormatString


I have a column when I bind enum:

<telerik:GridViewComboBoxColumn Header="N/U" DataMemberBinding="{Binding VehicleCondition}" ItemsSourceBinding="{Binding Source={StaticResource Locator}, Path=CarSalon.VehicleConditions}" IsGroupable="False" DataFormatString="" />

How can I display only first letter by DataFormatString?

Or maybe another solution without DataFormatString?


Solution

  • In this case you want to implement a ValueConverter which will look something like this (using LINQ string extensions):

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((string)value).First().ToString();
    }
    

    Obviously if your input value (VehicleCondition) isn't a string you'll need to do something more complicated.

    Your XAML will become something like this:

    <telerik:GridViewComboBoxColumn Header="N/U" DataMemberBinding="{Binding VehicleCondition, Converter={StaticResource initialLetterConverter}}" ...
    

    If you need to access other information about the item not just the VehicleCondition then you can change the binding to:

    <telerik:GridViewComboBoxColumn Header="N/U" DataMemberBinding="{Binding, Converter={StaticResource initialLetterConverter}}" ...
    

    which will bind to the object. Your converter then becomes something like:

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var carSalon = (CarSalon)value;
        string result = string.Empty;
    
        if (carSalon != null && <whatever else you need to test>)
        {
            result = temp.VehicleCondition.First().ToString();
        }
    
        return result;
    }
    

    where you can do any checks on the object or get other properties of the object you need.