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;
}