Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF PropertyGrid supports multiple selection

Is this documentation still valid or am I missing something?

http://doc.xceedsoft.com/products/XceedWpfToolkit/Xceed.Wpf.Toolkit~Xceed.Wpf.Toolkit.PropertyGrid.PropertyGrid~SelectedObjects.html

PropertyGrid control does not appear to have SelectedObjects or SelectedObjectsOverride members. I'm using the latest version (2.5) of the Toolkit against .NET Framework 4.0.

UPDATE

@faztp12's answer got me through. For anyone else looking for a solution, follow these steps:

  1. Bind your PropertyGrid's SelectedObject property to the first selected item. Something like this:

    <xctk:PropertyGrid PropertyValueChanged="PG_PropertyValueChanged" SelectedObject="{Binding SelectedObjects[0]}"  />
    
  2. Listen to PropertyValueChanged event of the PropertyGrid and use the following code to update property value to all selected objects.

    private void PG_PropertyValueChanged(object sender, PropertyGrid.PropertyValueChangedEventArgs e)
    {
      var changedProperty = (PropertyItem)e.OriginalSource;
    
      foreach (var x in SelectedObjects) {
        //make sure that x supports this property
        var ProperProperty = x.GetType().GetProperty(changedProperty.PropertyDescriptor.Name);
    
        if (ProperProperty != null) {
    
          //fetch property descriptor from the actual declaring type, otherwise setter 
          //will throw exception (happens when u have parent/child classes)
          var DeclaredProperty = ProperProperty.DeclaringType.GetProperty(changedProperty.PropertyDescriptor.Name);
    
          DeclaredProperty.SetValue(x, e.NewValue);
        }
      }
    }
    

Hope this helps someone down the road.

like image 447
dotNET Avatar asked Oct 30 '22 17:10

dotNET


1 Answers

What i did when i had similar problem was I subscribed to PropertyValueChanged and had a List filled myself with the SelectedObjects.

I checked if the contents of the List where of the same type, and then if it is so, I changed the property in each of those item :

PropertyItem changedProperty = (PropertyItem)e.OriginalSource;
PropertyInfo t = typeof(myClass).GetProperty(changedProperty.PropertyDescriptor.Name);
                if (t != null)
                {
                    foreach (myClass x in SelectedItems)
                        t.SetValue(x, e.NewValue);
                }

I used this because i needed to make a Layout Designer and this enabled me change multiple item's property together :)

Hope it helped :)

Ref Xceed Docs

like image 70
fahimalizain Avatar answered Nov 10 '22 14:11

fahimalizain