Silverlight 4.0 Silverlight 4文本框中的字符大小写

Silverlight 4.0 Silverlight 4文本框中的字符大小写,silverlight-4.0,textbox,Silverlight 4.0,Textbox,我正在编写Silverlight 4业务应用程序,遇到了一个问题。我需要文本框中的文本输入被强制为大写。我从各种论坛了解到Silverlight没有实现CharacterCasing和CSS样式 有没有其他方法可以做到这一点 您可以通过创建一个行为来实现这一点,如: public class UpperCaseAction : TriggerAction<TextBox> { protected override void Invoke(object parameter)

我正在编写Silverlight 4业务应用程序,遇到了一个问题。我需要文本框中的文本输入被强制为大写。我从各种论坛了解到Silverlight没有实现CharacterCasing和CSS样式


有没有其他方法可以做到这一点

您可以通过创建一个行为来实现这一点,如:

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;
    }
}
<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>
要创建和使用行为,您需要下载并安装,并添加对的引用。

请尝试以下操作:

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\u KeyDown(对象发送方,KeyEventArgs e)
{
e、 Handled=MakeUpperCase((文本框)sender,e);
}
bool MakeUpperCase(文本框txt,KeyEventArgs e)
{
如果(Keyboard.Modifiers!=ModifierKeys.None | | |(e.KeyKey.Z))//不处理ModifierKeys(使用shift键)
{
返回false;
}
其他的
{
字符串n=新字符串(新字符[]{(字符)e.PlatformKeyCode});
int nSelStart=txt.SelectionStart;
txt.Text=txt.Text.Remove(nSelStart,txt.SelectionLength);//从开始到结束选择中删除字符
txt.Text=txt.Text.Insert(nSelStart,n);//插入值n
选择(开始+1,0);//用于cursortext
返回true;//停止在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

    }
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
    }

}