开发者

Character Casing in Silverlight 4 TextBox

开发者 https://www.devze.com 2023-01-23 06:40 出处:网络
I am writing a Silverlight 4 business application and have run into an issue. I need the text input in TextBoxes to be forced to UpperCase. What I understand from various forums is Silverlight does no

I am writing a Silverlight 4 business application and have run into an issue. I need the text input in TextBoxes to be forced to UpperCase. What I understand from various forums is Silverlight does not Implement CharacterCasing and CSS Styling.

Is there any other wa开发者_如何学Goy to achieve this?


You can achieve this by creating a behavior, like this:

public class UpperCaseAction : TriggerAction<TextBox>
{

    protected override void Invoke(object parameter)
    {
        var selectionStart = AssociatedObject.SelectionStart;
        var selectionLenght = AssociatedObject.SelectionLength;
        AssociatedObject.Text = AssociatedObject.Text.ToUpper();
        AssociatedObject.SelectionStart = selectionStart;
        AssociatedObject.SelectionLength = selectionLenght;
    }
}

Then, use it in your TextBox, like this:

<Grid x:Name="LayoutRoot" Background="White">
    <TextBox TextWrapping="Wrap" VerticalAlignment="Top" Margin="10">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="TextChanged">
                <ASD_Answer009_Behaviors:UpperCaseAction/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>
</Grid>

Where i: is a namespace for

clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity

Code behind:

System.Windows.Interactivity.EventTrigger eventTrigger = new System.Windows.Interactivity.EventTrigger("TextChanged");
eventTrigger.Actions.Add(new UpperCaseAction());   
System.Windows.Interactivity.Interaction.GetTriggers(myTextBox).Add(eventTrigger);

In order to create and use behaviors, you need to download and install the Expression Blend SDK for Silverlight 4 and add a reference to System.Windows.Interactivity.dll.


Try this:

private void txt2_KeyDown(object sender, KeyEventArgs e)
{
    e.Handled = MakeUpperCase((TextBox)sender, e);
}

bool MakeUpperCase(TextBox txt, KeyEventArgs e)
{
    if (Keyboard.Modifiers != ModifierKeys.None || (e.Key < Key.A) || (e.Key > Key.Z))  //do not handle ModifierKeys (work for shift key)
    {
        return false;
    }
    else
    {
        string n = new string(new char[] { (char)e.PlatformKeyCode });
        int nSelStart = txt.SelectionStart;

        txt.Text = txt.Text.Remove(nSelStart, txt.SelectionLength); //remove character from the start to end selection
        txt.Text = txt.Text.Insert(nSelStart, n); //insert value n
        txt.Select(nSelStart + 1, 0); //for cursortext

        return true; //stop to write in txt2
    }

}


    private void txt2_KeyDown(object sender, KeyEventArgs e)
    {

        if (Keyboard.Modifiers != ModifierKeys.None) return; //do not handle ModifierKeys (work for shift key)

        string n = new string(new char[] { (char)e.PlatformKeyCode });
        int nSelStart = txt2.SelectionStart;

        txt2.Text = txt2.Text.Remove(nSelStart, txt2.SelectionLength); //remove character from the start to end selection
        txt2.Text = txt2.Text.Insert(nSelStart, n); //insert value n
        txt2.Select(nSelStart + 1, 0); //for cursortext

        e.Handled = true; //stop to write in txt2

    }
0

精彩评论

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