开发者

WPF - clear all textboxes in tabcontrol not working

I have an application with a tab control and several textboxes in each tab and when the user says so, I would like every text box in the window (called MainWindow) to be cleared. I used the method described her开发者_JAVA百科e, but it only seems to work for the textboxes in the tab that it is in focus.


Try this:

void ClearTextBoxes(DependencyObject obj)
{
    TextBox tb = obj as TextBox;
    if (tb != null)
        tb.Text = "";

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj as DependencyObject); i++)
        ClearTextBoxes(VisualTreeHelper.GetChild(obj, i));
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    ClearTextBoxes(this);
}


Try replace calls to VisualTreeHelper.GetChildren with LogicalTreeHelper.GetChildren

LogicalTreeHelper gets the actual visual tree. usually this is way more than the logical tree, but in this case since the other tabs are not visible - the visual sub-tree in those tabs doesn't get created. The LogicalTree should still be there though, so that should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜