WPF ComboBox,强制输入大写

发布于 2024-09-06 10:11:11 字数 222 浏览 6 评论 0原文

我有一个带有 TextSearchEnabled 的可编辑 WPF ComboBox。当用户键入以过滤组合框时,我需要强制用户的文本输入为大写。

我正在考虑修改作为控件一部分的文本框(名为“PART_EditableTextBox”)以设置 CharacterCasing="Upper",但是我不太清楚如何做到这一点。

我是否需要使用触发器或以某种方式修改模板?

I have an editable WPF ComboBox with TextSearchEnabled. I need to force the user's text input to uppercase when they type to filter the ComboBox.

I was thinking of modifying the textbox that is part of the control (named 'PART_EditableTextBox') to set CharacterCasing="Upper", however I can't quite figure out how to do this.

Do I need to use a trigger, or modify the template in some way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沒落の蓅哖 2024-09-13 10:11:11

这有效并且似乎是一个合理的解决方案:

protected void winSurveyScreen_Loaded(object sender, RoutedEventArgs e)
{
    (comboBox.Template.FindName("PART_EditableTextBox", cbObservation) as TextBox).CharacterCasing = CharacterCasing.Upper;
}

确保组合框在加载时不会折叠,否则将找不到模板。

This works and seems like a reasonable solution:

protected void winSurveyScreen_Loaded(object sender, RoutedEventArgs e)
{
    (comboBox.Template.FindName("PART_EditableTextBox", cbObservation) as TextBox).CharacterCasing = CharacterCasing.Upper;
}

Ensure that the combobox is not collapsed on loaded otherwise the template will not be found.

帅气尐潴 2024-09-13 10:11:11

IMO,更快的方法是将 UpdateTrigger 设置为 PropertyChanged,并在数据对象中,更新时将值大写。

IMO, the quicker way is to set the UpdateTrigger to PropertyChanged and, in the data object, uppercase the value when it is updated.

土豪 2024-09-13 10:11:11

我发现使用附加属性的 post 。这允许将其用于所有 ComboBox,而无需重写代码。

I found that post where the attached property is used. That permit to use that for all of your ComboBox without rewriting the code.

我的黑色迷你裙 2024-09-13 10:11:11
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    Textbox editableTextbox = sender as Textbox;
    foreach (char ch in e.Text)
    {
        if (Char.IsLower(ch))
        {
            editableTextbox.Text += Char.ToUpper(ch);
            e.Handled = true;
        }
    }
}

或者尝试创建一个附加文本框的行为

private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    Textbox editableTextbox = sender as Textbox;
    foreach (char ch in e.Text)
    {
        if (Char.IsLower(ch))
        {
            editableTextbox.Text += Char.ToUpper(ch);
            e.Handled = true;
        }
    }
}

or try creating an attached behaviour for the textbox

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文