使用TextBox的索引在StackPanel中选择XAML TextBox

时间:2016-02-06 11:53:33

标签: c# xaml textbox win-universal-app stackpanel

我有以下代码:

//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?

提前致谢。

1 个答案:

答案 0 :(得分:1)

也许有一个更优雅的解决方案,但这应该有效:

        //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);