开发者

Only first letter by DataFormatString

开发者 https://www.devze.com 2023-04-07 15:54 出处:网络
I have a column when I bind enum: <telerik:GridViewComboBoxColumn Header=\"N/U\" DataMemberBinding=\"{Binding VehicleCondition}\" ItemsSourceBinding=\"{Binding Source={StaticResource Locator}, Pat

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 DataForm开发者_如何学PythonatString?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号