【问题标题】:Select XAML TextBox in StackPanel using TextBox's Index使用 TextBox 的索引在 StackPanel 中选择 XAML TextBox
【发布时间】:2016-02-06 20:16:41
【问题描述】:

我有以下代码:

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

提前致谢。

【问题讨论】:

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


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-13
    • 2013-03-29
    • 2012-10-26
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    相关资源
    最近更新 更多