使用 TextBox 的索引在 StackPanel 中选择 XAML TextBox [英] Select XAML TextBox in StackPanel using TextBox's Index
本文介绍了使用 TextBox 的索引在 StackPanel 中选择 XAML TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
//All elements edited below have already been defined in MainPage.xaml
int index = Window_1_Document_Page_1.Children.IndexOf(sender as TextBox);
//Window_1_Document_Page_1.Focus(FocusState.Programmatic);
Window_1_Document_Page_1.Children.Remove(sender as TextBox);
在删除sender as TextBox
之前,如何将焦点设置到它上面的TextBox?
Before deleting sender as TextBox
, how do I set the focus to the TextBox above it?
提前致谢.
推荐答案
也许有更优雅的解决方案,但这应该有效:
Maybe there is a more elegant solution, but this should work:
//Get index of control you want to delete
int index = Panel.Children.IndexOf(sender as TextBox);
//find textbox with lower index
var control = Panel.Children.LastOrDefault(c => c is TextBox && Panel.Children.IndexOf(c) < index);
//check if a control was found
if (control != null)
{
//set focus
var textbox = control as TextBox;
textbox.Focus(FocusState.Programmatic);
}
Panel.Children.Remove(sender as TextBox);
这篇关于使用 TextBox 的索引在 StackPanel 中选择 XAML TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文