Silverlight的AutoCompleteBox大写输入 [英] Silverlight AutoCompleteBox uppercase input

查看:141
本文介绍了Silverlight的AutoCompleteBox大写输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要强制里面AutoCompleteBox大写输入在Silverlight 4应用程序

I need to force uppercase input inside AutoCompleteBox in Silverlight 4 app.

在文本框,这可以通过在KeyDown事件像更换Text属性来完成:

In TextBox this could be done by replacing Text property on KeyDown event like:

            control.Text += enteredChar;
            control.Select(control.Text.Length, 0); //To maintain caret position



不过AutoCompleteBox不提供文本选择功能,我发现没有办法。移动插入符字符串结束

However AutoCompleteBox doesn't provide text selection function, and I found no way to move caret to string end.

推荐答案

您需要更改AutoCompleteBox模板中添加UpperCaseBehavior到TextBox:

You need to change AutoCompleteBox template and add UpperCaseBehavior to the TextBox within:

<TextBox x:Name="Text" ...>
    <i:Interaction.Behaviors>
        <behaviors:UpperCaseBehavior/>
    </i:Interaction.Behaviors>
</TextBox>

其中UpperCaseBehavior为:

where UpperCaseBehavior is:

public class UpperCaseBehavior: Behavior<TextBox>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.TextChanged += AssociatedObject_TextChanged;
    }

    private void AssociatedObject_TextChanged(object sender, TextChangedEventArgs args)
    {
        var selectionStart = AssociatedObject.SelectionStart;
        var selectionLength = AssociatedObject.SelectionLength;

        AssociatedObject.Text = AssociatedObject.Text.ToUpper();

        AssociatedObject.SelectionStart = selectionStart;
        AssociatedObject.SelectionLength = selectionLength;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.TextChanged -= AssociatedObject_TextChanged;
        base.OnDetaching();
    }
}

这篇关于Silverlight的AutoCompleteBox大写输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆