如何自动更改Silverlight文本框中的文本? [英] How to automatically change text in a silverlight textbox?

查看:75
本文介绍了如何自动更改Silverlight文本框中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Silverlight 5项目中使用MVVM / Caliburn.Micro,并且我要求将用户在Silverlight文本框中输入的文本自动更改为大写。

I am using MVVM/Caliburn.Micro in a silverlight 5 project and I have a requirement to automatically change the text the user enters in a silverlight textbox to uppercase.

首先,我想我可以将ViewModel上的backing变量设置为大写,并且双向绑定将更改文本。那没用(尽管我相信如果我使用丢失焦点事件会那样,但是我不能这样做,因为我还有其他必须为KeyUp做的事情,并且附加两个事件会导致xaml错误)

First, I thought I could just set the backing variable on the ViewModel to uppercase and the two way binding would change the text. That didn't work (though I believe it will if I use a lost focus event, but I cannot do that since I have other things I must do for KeyUp as well and attaching two events results in a xaml error)

由于无法正常工作,我尝试在KeyUp事件上调用方法。 在技术上是有效的,但是由于它替换了文本,因此将光标放回了开头,因此用户最终向后键入。

Since that didn't work I tried calling a method on the KeyUp event. This technically works, but since it is replacing the text it puts the cursor back at the beginning, so the user ends up typing backwards.

这似乎是相当简单的功能-如何将用户键入的文本转换为大写?我想念一些简单的东西吗?

This seems like fairly simple functionality - how do I transform the text a user types into uppercase? Am I missing something easy?

这是我现有的代码。 Xaml:

Here is my existing code. Xaml:

<TextBox x:Name="SomeName" cal:Message.Attach="[Event KeyUp] = [Action ConvertToUppercase($eventArgs)]" />

查看模型:

public void ConvertToUppercase(System.Windows.Input.KeyEventArgs e)
{
     SomeName = _someName.ToUpper();
     //Other code that doesn't concern uppercase
}





编辑其他解决方案:
McAden提出了一个不错的通用解决方案。我也同时意识到有一个替代解决方案(只需将文本框作为参数传递给大写方法并移动光标),所以这也是代码:



EDIT FOR ALTERNATE SOLUTION: McAden put forth a nice generic solution. I also realized at about the same time that there was an alternate solution (just pass the textbox as a param to the uppercase method and move the cursor), so here is the code for that as well:

xaml:

<TextBox x:Name="SomeName" cal:Message.Attach="[Event KeyUp] = [Action ConvertToUppercase($source, $eventArgs)]; [Event KeyDown] = [Action DoOtherStuffThatIsntQuestionSpecific($eventArgs)]" />

cs方法:

public void ConvertToUppercase(TextBox textBox, System.Windows.Input.KeyEventArgs e)
{
    //set our public property here again so the textbox sees the Caliburn.Micro INPC notification fired in the public setter
    SomeName = _someName.ToUpper();

    //move the cursor to the last so the user can keep typing
    textBox.Select(SomeName.Length, 0);
}

当然还有标准的Caliburn.Micro属性:

and of course cs standard Caliburn.Micro property:

private String _someName = "";
public String SomeName
{
    get
    {
        return _someName;
    }
    set
    {
        _someName = value;
        NotifyOfPropertyChange(() => SomeName);
    }
}


推荐答案

创建此处中提到的ToUpper EventTrigger。还要为您要完成的其他功能创建另一个。将它们都添加到xaml中:

Create a ToUpper EventTrigger as mentioned here. Also create another one for whatever otherfunctionality you're trying to accomplish. Add them both in xaml:

<TextBox Text="{Binding SomeName, Mode=TwoWay}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="TextChanged">
            <myBehaviors:UpperCaseAction/>
        </i:EventTrigger>
        <i:EventTrigger EventName="TextChanged">
            <myBehaviors:MyOtherAction/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

编辑:我已经使用以下命令(不涉及任何隐藏代码

EDIT: I've fully tested this solution using the following (NO code-behind is involved)

大写操作:

public class UpperCaseAction : TriggerAction<TextBox>
{
    protected override void Invoke(object parameter)
    {
        var selectionStart = AssociatedObject.SelectionStart;
        var selectionLength = AssociatedObject.SelectionLength;
        AssociatedObject.Text = AssociatedObject.Text.ToUpper();
        AssociatedObject.SelectionStart = selectionStart;
        AssociatedObject.SelectionLength = selectionLength;
    }
}

其他操作:

public class OtherAction : TriggerAction<TextBox>
{
    Random test = new Random();

    protected override void Invoke(object parameter)
    {
        AssociatedObject.FontSize = test.Next(9, 13);
    }
}

XAML名称空间(在这种情况下为 TestSL测试项目的名称空间-适当地使用您的名称空间):

XAML namespaces (TestSL in this case being the namespace of my test project - use your namespace as appropriate):

xmlns:local="clr-namespace:TestSL"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

XAML文本框

<Grid x:Name="LayoutRoot" Background="LightGray" Width="300" Height="200">
    <TextBox TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10" Width="100">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="TextChanged">
                <local:UpperCaseAction />
            </i:EventTrigger>
            <i:EventTrigger EventName="TextChanged">
                <local:OtherAction />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>
</Grid>

这篇关于如何自动更改Silverlight文本框中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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