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

How can I select a specific property in the property grid in code

Sep 18, 2012 at 11:29 PM

The PropertyGrid does not appear to check and/or honor the DefaultProperty attribute when it is applied to a class. We have a specific property that we want to be the active property when the selected object is changed. I tried to set the SelectedProperty but it is read only.

Sep 19, 2012 at 1:56 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Sep 19, 2012 at 2:05 PM

The following issues were created based on your feedback:

http://wpftoolkit.codeplex.com/workitem/18663

http://wpftoolkit.codeplex.com/workitem/18664

Sep 19, 2012 at 11:57 PM
Edited Sep 20, 2012 at 12:52 PM

Hello,

As a work-around, I wrote some PropertyGrid extension methods and then use Reflection to set the SelectedProperty and the PropertyItem.IsSelected property through code. The code and some sample calling code is shown below. The PropertyGrid SelectedProperty is being updated correctly, but the corresponding property name (eg FirstName or LastName) in the PropertyGrid on the left side is not highlighting. Any ideas on how to get the corresponding property name to be highlighted when I set the SelectedProperty in code?

NOTE: When I added propertyItem.IsSelected = true; to the last SetSelectedProperty overload below, highlighting  worked as exptected.

For testing purposes, I have a Person class with FirstName, LastName, Address1, Address2, City, State and ZipCode defined. On the left side of my WPF form I have a canvas with Labels and TextBoxes for the above bound to the DataContext. The DataContext for the form is set to the selected Person class from a list of Persons. On the right side, I have a PropertyGrid in which the SelectedObject is set to the corresponding Person class. When a user DoubleClicks one of the TextBoxes, I want to select the corresponding Property in the PropertyGrid and highlight the Property Name on the left side of the PropertyGrid similarly to the way the PropertyGrid behaves when a user clicks a given property to select it.

PropertyGrid ExtensionMethods

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

using Xceed.Wpf.Toolkit.PropertyGrid;

namespace PropertyGridTesting.ExtensionMethods
{
    public static class PropertyGridExtensionMethods
    {
        private static Dictionary<PropertyGrid, PropertyItem> _defaultProperties = null;

        static PropertyGridExtensionMethods()
        {
            _defaultProperties = new Dictionary<PropertyGrid, PropertyItem>();
        }

        public static PropertyItem SetDefaultProperty(this PropertyGrid propertyGrid, string propertyName)
        {
            PropertyItem defaultProperty = _defaultProperties.ContainsKey(propertyGrid) ? _defaultProperties[propertyGrid] : null;
            if (defaultProperty == null)
            {
                defaultProperty = GetProperty(propertyGrid, propertyName);
                _defaultProperties.Add(propertyGrid, defaultProperty);
            }

            return defaultProperty;
        }

        public static void SetSelectedProperty(this PropertyGrid propertyGrid)
        {
            SetSelectedProperty(propertyGrid, _defaultProperties[propertyGrid]);
        }

        public static void SetSelectedProperty(this PropertyGrid propertyGrid, string propertyName)
        {
            SetSelectedProperty(propertyGrid, GetProperty(propertyGrid, propertyName));
        }

        public static void SetSelectedProperty(this PropertyGrid propertyGrid, PropertyItem propertyItem)
        {
            PropertyInfo property = propertyGrid.GetType().GetProperty("SelectedProperty", BindingFlags.Public | BindingFlags.Instance);
            property.SetValue(propertyGrid, propertyItem, null);
            propertyItem.IsSelected = true;
        }

        private static PropertyItem GetProperty(PropertyGrid propertyGrid, string propertyName)
        {
            if (propertyGrid.Properties != null)
            {
                foreach (PropertyItem item in propertyGrid.Properties)
                {
                    if (item.BindingPath.Equals(propertyName))
                    {
                        return item;
                    }
                }
            }
            else if (propertyGrid.PropertyDefinitions != null)
            {
                foreach (var item in propertyGrid.PropertyDefinitions)
                {
                    
                }
            }

            return null;
        }
    }
}
Calling Code
private void textBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    TextBox textBox = e.Source as TextBox;
    if (textBox != null)
    {
        BindingExpression binding = textBox.GetBindingExpression(TextBox.TextProperty);
        string bindingPath = binding.ParentBinding.Path.Path;
        propertyGrid1.SetSelectedProperty(bindingPath);
    }
}
Developer
Oct 3, 2012 at 3:58 PM

The DefaultPropertyAttribute will be managed in the v1.8.

Nov 7, 2012 at 7:45 PM

Fixed in v1.8

Nov 14, 2013 at 1:00 PM
Hi! I'm attempting to pre-select/pre-hightlight a given property item when the user first enters my dialog. In searching around, it appeared that this DefaultPropertyAttribute would be the way to do that? But, I'm not seeing any examples of how it's used. I would have thought I would have simply been able to add it to my other properties like "DisplayName", "Category", etc, but it's not allowing me to do that. It appears to be valid on class declarations? I tried adding it above my data class and I still am not seeing anything pre-highlighted. Would you be able to point me to an example of how this feature is used? Thanks in advance, -Glenn
Developer
Nov 14, 2013 at 1:55 PM
Hi,

You are right, simply adding the DefaultPropertyAttribute on the class being used as the PropertyGrid.SelectedObject will do the job.
 [DefaultProperty("ID")]
  public class Person
  {
    public string FirstName
    {
      get;
      set;
    }
    public string LastName
    {
      get;
      set;
    }
    public int ID
    {
      get;
      set;
    }
  }

---------------------------
 _propertyGrid.SelectedObject = new Person()
      {
        FirstName = "Bob",
        LastName = "Jones",
        ID = 34
      };
When opening the propertyGrid, you should see the ID property being highlighted.
Nov 14, 2013 at 6:01 PM
Of course. Worked like a charm! Thank you!