开发者

i want to clear all values in grid not (datagrid) children in wpf

i want to clear all values in grid children ex开发者_如何学运维ample if one of my grid children is textbox I want to reset it or clear it fro grid name not from textbox ? how? not clear the grid but clear the text in the textbox


The following code should clear all TextBoxes:

var textboxes = grid.Children.OfType<TextBox>();
foreach (var textBox in textboxes)
    textBox.Text = String.Empty;


Next function will search for all controls of specified type in specified object:

/// <summary>
/// Helper function for searching all controls of the specified type.
/// </summary>
/// <typeparam name="T">Type of control.</typeparam>
/// <param name="depObj">Where to look for controls.</param>
/// <returns>Enumerable list of controls.</returns>
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj)
    where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }

            foreach (T childOfChild in FindVisualChildren<T>(child))
            {
                yield return childOfChild;
            }
        }
    }
}

Thanks to author of this function... Don't remember where I took it.

Now you can for example clear values of all text boxes:

foreach (TextBox child in FindVisualChildren<TextBox>(yourGrid))
{
    child.Text = string.Empty;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜