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}}"/>