2

旧版应用转换问题。VB6 TextBox_KeyDown() 允许更改键(例如强制击键为大写,但还有许多其他用途)。如何在 WPF 中做到这一点?

我能看到的唯一方法就是处理所有 TextBox 击键。实际上,重新实现 TextBox 编辑。我宁愿不去那里。

4

1 回答 1

0

非常快速和肮脏的解决方案。假设您想将 TextBox.Text 值绑定到某个东西,您可以编写一个转换器,它只需在字符串上调用 ToUpper()。

在下面的示例中,文本框绑定到自身。这很可能不是您在生产中想要的,但它可能会激发灵感。

<local:UpperConverter x:Key="toUpperConverter" />

...

<TextBox Text="{Binding RelativeSource={RelativeSource Mode=Self},
                                Path=Text, Mode=OneWay, Converter={StaticResource toUpperConverter},
                                UpdateSourceTrigger=PropertyChanged}" />

...

class UpperConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.ToString().ToUpper();
        }
于 2010-05-19T12:40:16.837 回答