How to find a child (Gridviewcolumn) of Listview / MVVM

260 views Asked by At

inside a behavior (connected to windows.interactivity) im trying to find a child of a Listview - specifically Gridviewcolumn- to be able to resize its width, the code is built using MVVM without code-behind.

the problem is simply that using the code below the Gridviewcolumn is not found, meanwhile other children of the Maingrid , f.e. a Button, it would be no problem (see the button example below).

any suggestions where the mistake comes from? thanks in advance!

// _parent is the main Grid of the Window

Button button = GetTemplateChildByName(_parent); // for the button its working and the value is returned


GridViewColumn gridviewcolumn = GetTemplateChildByNames(_parent);// for the  GridViewColumn  its not working and no value is returned

....



  public Button GetTemplateChildByName(DependencyObject parent)
        {
            int childnum = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childnum; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                if (child is Button)
                {
                    return child as Button;
                }
                else
                {
                    var s = GetTemplateChildByName(child);
                    if (s != null)
                        return s;
                }
            }
            return null;
        }

     public GridViewColumn GetTemplateChildByNames(DependencyObject parent)
        {
            int childnum = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childnum; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                if (child is GridViewColumn)
                {
                    return child as GridViewColumn;
                }
                else
                {
                    var s = GetTemplateChildByNames(child);
                    if (s != null)
                        return s;
                }
            }
            return null;
        }
0

There are 0 answers