Multiple PropertyGrids on the same page

Dec 16, 2009 at 3:54 PM
Edited Dec 16, 2009 at 3:55 PM

First of all, great job with the Property Grid! It's a really handy utility.

I've ran into an issue however. If multiple Propertygrids are instantiated naming conflicts occur easily when binding to objects with properties having the same name bacause of the way labels are named. In ValueEditorBase.cs, line 20 the code is:

this.Label.Name = "lbl" + property.Name;

If the control name itself was added in this name, there would be no naming conflicts. However the resolving this issue isn't as simple as that as the issue runs through the system.

It would be great if at some point the following scenario would be supported:

PropertyGrid PropertyGrid1;
PropertyGrid PropertyGrid2; // These are declared in XAML
Label label1 = new Label();
Label label2 = new Label();
PropertyGrid1.SelectedObject = label1;
PropertyGrid2.SelectedObject = label2; 

Please let me know if you know a workaround to achieve this with the current code base or if you're planning to add future support for this.


Dec 17, 2009 at 1:13 PM

Thanks for the positive feedback and the great question. The simple answer is you're correct and that line should be removed. Actually that one, and the one for the Value Editor itself. I've pasted some code showing the modified Constructor method. Additionally, neither of these statements are necessary as the xaml engine will auto-assign unique names.

public ValueEditorBase(PropertyGridLabel label, PropertyItem property)
	this.Label = label;
	//this.Label.Name = "lbl" + property.Name; --Removed to allow multiple instances of the property grid to be associated with the same property.
this.Label.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(Label_MouseLeftButtonDown); this.Label.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(Label_MouseLeftButtonUp); if (!property.CanWrite) this.Label.Foreground = new SolidColorBrush(Colors.Gray); //this.Name = "txt" + property.Name; --Removed to allow multiple instances of the property grid to be associated with the same property.
this.Property = property; this.BorderThickness = new Thickness(0); this.Margin = new Thickness(0); this.HorizontalAlignment = HorizontalAlignment.Stretch; this.HorizontalContentAlignment = HorizontalAlignment.Stretch; }
Jan 8, 2010 at 1:05 PM

Silly of me not to notice this... thanks for the help, it worked!

Jan 8, 2010 at 1:06 PM

No problem, glad I could help.