如何在 WPF 中设置列​​表框项的字体大小写? [英] How to set the font case of a listbox item in WPF?

查看:23
本文介绍了如何在 WPF 中设置列​​表框项的字体大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WPF 窗口中有一个 ListBox.根据ComboBox 的选定项,从数据库中检索ListBox 项并将其绑定为ListBox 的ItemSource.我想更改 ListBox 项目的大小写,即当我绑定所有项目时都是大写的.我想将大小写更改为仅将单词的首字母大写.

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.

推荐答案

您需要一个转换器来实现此行为.

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();;
    }
}

将此添加为资源

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

并在 XAML 中使用它作为

and use it in XAML as

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

这篇关于如何在 WPF 中设置列​​表框项的字体大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆