Select XAML TextBox in StackPanel using TextBox's Index

心不动则不痛 提交于 2019-12-12 05:29:18

问题


I have the following code:

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

Before deleting sender as TextBox, how do I set the focus to the TextBox above it?

Thanks in advance.


回答1:


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


来源:https://stackoverflow.com/questions/35240682/select-xaml-textbox-in-stackpanel-using-textboxs-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!