开发者

How do I uppercase using ItemStringFormat in WPF?

开发者 https://www.devze.com 2023-03-25 04:44 出处:网络
Suppose I have a combobox where items are binded to an array of strings. I want to use ItemStringFormat to display those strings in uppercase开发者_C百科. How do I do that?

Suppose I have a combobox where items are binded to an array of strings. I want to use ItemStringFormat to display those strings in uppercase开发者_C百科. How do I do that?

Update: I'm not completely clueless about formatting strings, but I've searched MSDN for a format specifier that will turn a string to uppercase, and for some reason I just can't find it! I would have expected it to be something like "{0:U}" or "{0:S}" or something like that.

I also can't believe I wasn't able to find the answer here on SO.


Sorry, it is not possible. However it is simple to achieve what you a DataTemplate and a value converter.

Example

<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>

Converter

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)
    {
    }
}

There is lots of information available on both of these topics on the internet and in any WPF book.


You can specify a Converter as part of your binding. It is trivial to build a class that implements IValueConverter which simply returns

  return ((string)value).ToUpper();


Is there a reason you can't just use .ToUpper(); ?

0

精彩评论

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

关注公众号