WPF PropertyGrid supports multiple selection

筅森魡賤 提交于 2019-12-10 13:54:56

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/33010077/wpf-propertygrid-supports-multiple-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!