Microsoft Developer Network > Página principal de foros > Windows Forms Designer > Is there any way to spin through the objects in a property grid
Formular una preguntaFormular una pregunta
 

RespondidaIs there any way to spin through the objects in a property grid

  • lunes, 10 de julio de 2006 23:10j2associates Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

    Hello all,

    Is there any kind of an items collection buried inside a PropertyGrid? I know there is a SelectedGridItem property, but I need to iterate through them and I have been unsuccessful in finding out a way to do that.

    Thanks in advance for any ideas and/or suggestions!

Respuestas

  • martes, 11 de julio de 2006 7:39Peter RitchieMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    The SelectedObjects property contains the objects whose properties are displayed in the grid.

    If you're not looking for objects in the property grid; but looking to iterate the properties of the object displayed in the property grid, you'd have to use the Type.GetProperties() method on each item in the PropertyGrid.SelectedProperties array.

  • miércoles, 15 de noviembre de 2006 11:27DirkProfi Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    Hello

    here a way to  iterate ...

    But how can I select a Property by set propertyGrid1.SelectedGridItem ???

     

            public string getPropertyInfoString(object obj)
            {
                StringBuilder result = new StringBuilder();

                Type t = Openimmo.GetType();
                PropertyDescriptorCollection col;
                col = TypeDescriptor.GetProperties(t);
                getPropertyInfo(ref result, obj, col, 0);
                return result.ToString();
            }


            private void getPropertyInfo(ref StringBuilder result, object obj, PropertyDescriptorCollection col, int level)
            {
                if (col == null)
                    return;
               
                foreach (PropertyDescriptor pd in col)
                {
                    if (pd.IsBrowsable == false || pd.IsReadOnly == true || pd.DesignTimeOnly == true)
                        continue;

                    result.Append("".PadLeft(level * 2));
                    result.Append(pd.Name);
                    result.Append(" = ");
                    result.Append(pd.GetValue(obj));
                    result.AppendLine();

                    object propObj = pd.GetValue(obj);
                    if (propObj == null)
                        continue;

                    Type propType = propObj.GetType();
                    if (propType.IsPublic && propType.IsClass)
                    {
                        if (propObj is IList)
                        {

                            IList list = (IList)propObj;
                            foreach (object item in list)
                            {
                                getPropertyInfo(ref result, item, TypeDescriptor.GetProperties(item), level + 1);                       
                            }
                        }
                        else
                            getPropertyInfo(ref result, propObj, TypeDescriptor.GetProperties(propObj), level + 1);
                    }

                }
            }

Todas las respuestas

  • martes, 11 de julio de 2006 7:39Peter RitchieMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    The SelectedObjects property contains the objects whose properties are displayed in the grid.

    If you're not looking for objects in the property grid; but looking to iterate the properties of the object displayed in the property grid, you'd have to use the Type.GetProperties() method on each item in the PropertyGrid.SelectedProperties array.

  • martes, 11 de julio de 2006 16:00joeycalisay Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    if you want to iterate on the items listed in the property grid though (top dropdown), you might want to iterate on the components in the designer surface through IDesignerHost perhaps...
  • martes, 11 de julio de 2006 18:13j2associates Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

    Hello Peter,

    Thanks for the response. Let's say that I have a class with 4 properties, Prop1, Prop2, Prop3 and Prop4. When the CollectionEditor pops up, the PropertyGrid on the right side will display the 4 properties for the currently selected item (assume only one for our discussion).

    The PropertyGrid exposes a SelectedGridItem property. I want to iterate all of the GridItems in the PropertyGrid. They obviously correspond to the class items which are listed in the left pane. But I don't know how to create a GridItem from a class property.

    What I want to do is get the GridItems from the PropertyGrid as if the user clicked on each of them in succession.

    Hello joey,

    To this point, I have not been successful in getting a DesignerHost. The various things I have tried have all returned Nothing.

  • miércoles, 15 de noviembre de 2006 11:27DirkProfi Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    Hello

    here a way to  iterate ...

    But how can I select a Property by set propertyGrid1.SelectedGridItem ???

     

            public string getPropertyInfoString(object obj)
            {
                StringBuilder result = new StringBuilder();

                Type t = Openimmo.GetType();
                PropertyDescriptorCollection col;
                col = TypeDescriptor.GetProperties(t);
                getPropertyInfo(ref result, obj, col, 0);
                return result.ToString();
            }


            private void getPropertyInfo(ref StringBuilder result, object obj, PropertyDescriptorCollection col, int level)
            {
                if (col == null)
                    return;
               
                foreach (PropertyDescriptor pd in col)
                {
                    if (pd.IsBrowsable == false || pd.IsReadOnly == true || pd.DesignTimeOnly == true)
                        continue;

                    result.Append("".PadLeft(level * 2));
                    result.Append(pd.Name);
                    result.Append(" = ");
                    result.Append(pd.GetValue(obj));
                    result.AppendLine();

                    object propObj = pd.GetValue(obj);
                    if (propObj == null)
                        continue;

                    Type propType = propObj.GetType();
                    if (propType.IsPublic && propType.IsClass)
                    {
                        if (propObj is IList)
                        {

                            IList list = (IList)propObj;
                            foreach (object item in list)
                            {
                                getPropertyInfo(ref result, item, TypeDescriptor.GetProperties(item), level + 1);                       
                            }
                        }
                        else
                            getPropertyInfo(ref result, propObj, TypeDescriptor.GetProperties(propObj), level + 1);
                    }

                }
            }

  • domingo, 27 de mayo de 2007 20:12trobinson Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

     

    Better late than never I guess..  Here is one way to do it.

    • if the SelectedObect property is not null then SelectedGridItem property will always be valid, even if the SelectedObject contains no browsable properties.
    • The GridItem object contains Parent, and GridItems properties.  This gives you a path to every GridItem on the PropertyGrid.

     

    In the code snippet below are two simple static functions.  One for getting back to the root parent from any GridItem and one for searching back through the GridItem Heiarchy  looking for a GridItem with a specific label.

     

    Here's a sample of using the functions:

    GridItem gridItemFind;

    String sPropName = "PropNameImLookngFor";

    gridItemFind = MyPropertyGrid.SelectedGridItem;

    if (gridItemFind != null)

    {

        gridItemFind = GetParentGridItem(gridItemFind);

        gridItemFind = FindGridItem(sPropName, gridItemFind);

        if (gridItemFind != null)

            MyPropertyGrid.SelectedGridItem = gridItemFind;

    }

     

     

    I Hope this helped,

    Travis Robinson

    www.picmicrochip.com

     

    Code Snippet

    private static GridItem GetParentGridItem(GridItem gItem)
    {
        // Takes a GridItem as a parameter and find the root(top most) GridItem
        GridItem gridItemFind = gItem;
        GridItem gridItemLast = null;

        while (gridItemFind != null)
        {
            gridItemLast = gridItemFind;
            gridItemFind = gridItemFind.Parent;
        }
        return gridItemLast;
    }

    private static GridItem FindGridItem(string sLabel, GridItem gridItemToSearch)
    {
        /*
        // Searches through gridItemToSearch and all of its child nodes
        // Returns the first GridItem that has a Label equal to the sLabel parameter.
        // NOTE:
        // You could use any matching criteria you wanted, the Label property is what
        // worked for me because it always equals the property name. You could also
        // use the gridItemToSearch.PropertyDescriptor
        */
        if (gridItemToSearch.Label == sLabel)
            return gridItemToSearch;
        if (gridItemToSearch.GridItems.Count > 0)
        {
            for (int iGrid = 0; iGrid < gridItemToSearch.GridItems.Count; iGrid++)
            {
                GridItem check = FindGridItem(sLabel, gridItemToSearch.GridItems[iGrid]);
                if (check != null)
                    return check;
            }
        }

        return null;
    }