How to set the font case of a listbox item in WPF?

时光怂恿深爱的人放手 提交于 2020-01-24 22:18:09

问题


I have a ListBox in a WPF Window. Based on the selected item of a ComboBox, the ListBox items are retrieved from database and bound as the ListBox's ItemSource. I want to change the case of the ListBox items, i.e., when i bind all the items are in uppercase. I want to change the case to capitalize only the starting letter of a word.


回答1:


You need a converter to achieve this behavior.

public class CaseConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {            
        TextInfo textInfo = culture.TextInfo;
        return  textInfo.ToTitleCase(value.ToString());
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();;
    }
}

Add this as resource

<Window.Resources>
    <local:CaseConverter x:Key="MyCaseConverter"></local:CaseConverter>
</Window.Resources>

and use it in XAML as

<TextBlock Text="{Binding Name, Converter={StaticResource MyCaseConverter}}"/>


来源:https://stackoverflow.com/questions/17802020/how-to-set-the-font-case-of-a-listbox-item-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!