How to find WinForms "GridItem" for C1PropertyGrid WPF edition

Posted by: Jeff.Huckins on 17 February 2022, 3:36 pm EST

  • Posted 17 February 2022, 3:36 pm EST

    I’m porting an application from WinForms to WPF. I am in the process of implementing Property Grid code from WinForms to WPF using the C1PropertyGrid WPF control.

    However, the WinForms control “selects” the default property in the property grid by iterating through the “GridItem” objects in the WinForms PropertyGrid.

    I need to know how to replicate this for the WPF C1PropertyGrid control. Here is the current WinForms code:

            /// <summary>
            /// Make sure the property grid is showing the default property
            /// </summary>
            /// <param name="o"></param>
            private void EnsurePropertyGridShowingDefaultProperty(object o)
            {
                AttributeCollection attributes = TypeDescriptor.GetAttributes(o);
                DefaultPropertyAttribute default_prop = (DefaultPropertyAttribute)attributes[typeof(DefaultPropertyAttribute)];
                PropertyGridSelectNamedProperty(default_prop.Name);
            }
    
            /// <summary>
            /// Select the named property
            /// </summary>
            /// <param name="sName"></param>
            private void PropertyGridSelectNamedProperty(string sName)
            {
                // Get root grid item
                GridItem giRoot = propertyGridAction.SelectedGridItem;
                if (giRoot == null) return;
                while (giRoot.Parent != null)
                    giRoot = giRoot.Parent;
    
                // Select grid item based upon name of default attribute
                GridItem giTarget = FindGridItem(sName, giRoot);
                if (giTarget == null)
                    giTarget = FindGridItem("Enabled", giRoot);
                if (giTarget != null)
                    propertyGridAction.SelectedGridItem = giTarget;
            }
    
            /// <summary>
            /// Find the property grid item with given name
            /// </summary>
            /// <param name="sName"></param>
            /// <param name="giStart"></param>
            /// <returns></returns>
            private static GridItem FindGridItem(string sName, GridItem giStart)
            {
                if (giStart == null)
                {
                    return null;
                }
    
                if (giStart.Label == sName)
                {
                    return giStart;
                }
    
                GridItem giTarget = null;
                for (int i = 0; i < giStart.GridItems.Count; i++)
                {
                    giTarget = FindGridItem(sName, giStart.GridItems[i]);
                    if (giTarget != null)
                    {
                        break;
                    }
                }
    
                return giTarget;
            }
    
  • Posted 17 February 2022, 3:59 pm EST

    I’m wondering if it is as simple as:

                
    PropertyBox propBox = propertyGridAction.GetPropertyPresenter(sName);
    
    if (propBox != null)
    {
    	propBox.Focus();
    
    
  • Posted 18 February 2022, 5:23 am EST

    Hi Jeff,

    Yes, you are right you need to use C1PropertyGrid’s GetPropertyPresenter method to get the property presenter i.e. PropertyBox as follows:

    
    var propertyBox = propertyGrid.GetPropertyPresenter("Id");
    if(propertyBox != null)
    {  
         // your code
    }
    
    

    Please refer to the attached sample for the same. (see PropertyGridSample.zip)

    Best Regards,

    Kartik

    PropertyGridSample.zip

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels