WPF属性网格支持多选。

5
这份文档是否仍然有效,或者我有所遗漏?

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. 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);
        }
      }
    }
    
希望这能帮助到后来者。

2
我使用的是相同的版本 2.5,但在 Xceed.Wpf.Toolkit.PropertyGrid 中找不到 SelectedObjects - fahimalizain
1
@faztp12 似乎只有 Plus 版本才有此功能。 - Кое Кто
@faztp12 请查看显示多个对象的常见属性 - Кое Кто
1个回答

2
当我遇到类似问题时,我订阅了PropertyValueChanged并手动填充了一个List,其中包含SelectedObjects
我检查了列表的内容是否为相同类型,如果是,则在每个项目中更改属性。
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文档

有趣。changeProperty是什么? - dotNET
这是随着事件 PropertyValueChanged 传入的参数 :) - fahimalizain
我在PropertyValueChanged事件中没有看到这样的参数。你确定你正在使用来自Extended WPF Tookit的PropertyGrid控件吗? - dotNET
抱歉 :P 已经编辑答案,加上了 changedProperty :) - fahimalizain
确实非常有帮助。这最终帮我解决了问题。非常感谢。还有一件事情我需要做,请查看我的编辑。 - dotNET

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接