2
在鼠标事件我试图创建TextBox,将其添加到网格,选择所有和焦点键盘。但不能得到它的工作:WPF麻烦获取TextBox集中
private void timeCodeEdit(object sender, MouseEventArgs e)
{
Grid grid = (Grid) ((Label) sender).Parent;
TextBox text = new TextBox();
text.Margin = new Thickness(0, 0, 75, 0);
text.Text = "aaaa";
grid.Children.Add(text);
text.LostFocus += lostFocus;
Keyboard.Focus(text);
text.SelectAll();
}
我试过Keyboard.Focus(text);
和text.Focus();
。如果我这样做:
private void lostFocus(object sender, RoutedEventArgs e)
{
Keyboard.Focus(sender as TextBox);
e.Handled = true;
}
我越来越StackOverflowException,导致它失去焦点后焦点。
也许有人可以帮助我吗?
尝试'FocusManager.SetFocusedElement(parentElement,txtBoxName)' – Gopichandar
我试过了,但它没有帮助 – Tom1410
你的代码对我来说工作正常;该文本框是专注,我可以直接输入。我尝试了'MouseEnter'和'MouseLeftButtonDown'事件。你使用的是什么.NET框架? – AjS