This project has moved and is read-only. For the latest updates, please go here.

There is a way to refresh propertyGrid without reassign the selectedObject

May 16, 2013 at 2:08 PM
Edited May 16, 2013 at 2:10 PM
Hi,

I implemented an expandable collection but when i add some objects to the collection by the propertyGrid, there is no update. So i implemented a reload function witch save the state of the propertyGrid properties, set the selectedObject to null and reassign the model. This method is not good in terms of performance and all the propertyItem are recreated. Is there a other way to do that?

Here a sample of code:
private void BackupState(PropertyItemCollection pProperties, List<Object> propertyList)
        {
            foreach (PropertyItem property in pProperties)
            {
                if (property.IsExpanded)
                {
                    List<Object> nodePropertyList = new List<Object>();
                    Dictionary<string, List<Object>> nodeDictionary = new Dictionary<string, List<Object>>();
                    if(property.Properties.Count > 0)
                        BackupState(property.Properties, nodePropertyList);
                    nodeDictionary.Add(property.PropertyDescriptor.Name, nodePropertyList);
                    propertyList.Add(nodeDictionary);
                }
            }
        }

        private void Restore(PropertyItemCollection pProperties, List<Object> propertyList)
        {
            foreach (PropertyItem property in pProperties)
            {
                if (property.IsExpandable)
                    foreach (Dictionary<string, List<Object>> dico in propertyList)
                    {
                        if (dico.ContainsKey(property.PropertyDescriptor.Name))
                        {
                            property.IsExpanded = true;
                            if(property.Properties.Count > 0)
                                Restore(property.Properties, dico[property.PropertyDescriptor.Name]);
                        }
                    }
            }
        }

private void ReloadPropertyGrid(Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid pMyPropertyGrid)
        {
            List<Object> myNodeList = new List<Object>();
            this.BackupState(pMyPropertyGrid.Properties, myNodeList);
            var tmp = propgrid.SelectedObject;
            propgrid.SelectedObject = null;
            propgrid.SelectedObject = tmp;
            this.Restore(pMyPropertyGrid.Properties, myNodeList);
        }
May 16, 2013 at 4:31 PM
did you try PropertyGrid.Update() method ?
May 17, 2013 at 8:41 AM
Edited May 17, 2013 at 1:59 PM
Yes i tried PropertyGrid.Update() but it doesn't work because when i add an item to my collection the propertyGrid need to create the propertyItem of the new element. (I've done an expandable collection).