c# - 如何在 WPF 中使用 ItemStringFormat 大写?

标签 c# wpf

假设我有一个组合框,其中的项目绑定(bind)到一个字符串数组。我想使用 ItemStringFormat 以大写形式显示这些字符串。我该怎么做?

更新:我对格式化字符串并非一无所知,但我在 MSDN 上搜索了一个格式说明符,可以将字符串转换为大写,但出于某种原因我就是找不到它!我原以为它会是“{0:U}”或“{0:S}”之类的东西。

我也无法相信我无法在 SO 上找到答案。

最佳答案

对不起,这是不可能的。然而,实现您的 DataTemplate 和值转换器很简单。

示例

<UserControl.Resources>
    <converters:StringToUpperCaseConverter x:Key="ToUpperConverter"/>
</UserControl.Resources>

<ComboBox ItemsSource={Binding YourCollection}>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text={Binding Path=YourValue, Converter="{StaticResource ToUpperConverter}}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
<ComboBox>

转换器

public class StringToUpperCaseConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((string)value).ToUpper();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
    }
}

在 Internet 和任何 WPF 书籍中都有大量关于这两个主题的信息。

关于c# - 如何在 WPF 中使用 ItemStringFormat 大写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6949308/

相关文章:

c# - Xamarin.Droid 在使用 Xamarin.Forms 时导致 Visual Studio 2015 中的构建错误

c# - 尝试转换 listbox.selecteditem 时为空引用?

C# WPF 机器相关的并发问题

c# - 如何使用C#从下面的字符串中提取键

c# - ASP :ImageButton OnClick event not firing in IE

C# - 获取字符串中的整数字节数组

c# - MonoTouch : App killed for low mem, 为什么?事件字节分配 5 MB 顶部

wpf - WPF 与 Caliburn Micro 的按键绑定(bind)

c# - 将 BIG IEnumerable 设置为 ListBox.ItemSsource 的最佳方法

c# - 如何从 DataTemplate 获取代码中的元素