如何在wpf中从文本框中聚焦光标?

时间:2012-12-06 18:32:19

标签: c# .net wpf textbox setfocus

<Grid x:Name="LayoutRoot">
    <TextBox x:Name="TxtFocusOut" Height="74" Margin="186,149,225,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="26.667" TextChanged="TextBox_TextChanged"/>
</Grid>

private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
    {
        if(TxtFocusOut.Text.Length>=4)
        {
            MessageBox.Show("Four you typed four values");
        }
    }

单击Ok Button On消息框后,光标应该从texbox中聚焦。但现在它在文本框中闪烁。我该怎么做才能从测试框中聚焦?

1 个答案:

答案 0 :(得分:1)

FocusManager.SetFocusedElement(AnotherElementID);  

通过这样做,这将失去焦点,焦点可以用于另一个元素 或
Keyboard.ClearFocus();
http://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.clearfocus.aspx
尝试其中任何一个

相关问题