WPF PropertyGrid 支持多选

WPF PropertyGrid supports multiple selection

此文档是否仍然有效,还是我遗漏了什么?

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

PropertyGrid 控件似乎没有 SelectedObjectsSelectedObjectsOverride 成员。我正在使用针对 .NET Framework 4.0 的最新版本 (2.5) 的工具包。

更新

@faztp12 的回答让我通过了。对于其他正在寻找解决方案的人,请按照以下步骤操作:

  1. 将您的 PropertyGridSelectedObject 属性 绑定到第一个选定的项目。像这样:

    <xctk:PropertyGrid PropertyValueChanged="PG_PropertyValueChanged" SelectedObject="{Binding SelectedObjects[0]}"  />
    
  2. 侦听 PropertyGridPropertyValueChanged 事件并使用以下代码更新所有选定对象的 属性 值。

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

希望这对以后的人有所帮助。

当我遇到类似问题时我所做的是我订阅了 PropertyValueChanged 并让 List 充满了 SelectedObjects.

我检查了 List 的内容是否属于同一类型,如果是,我更改了每个项目中的 属性 :

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

我使用它是因为我需要制作一个布局设计器,这使我能够一起更改多个项目的 属性 :)

希望对您有所帮助:)

参考 Xceed Docs