WPF PropertyGrid支持多项选择 [英] WPF PropertyGrid supports multiple selection

查看:262
本文介绍了WPF PropertyGrid支持多项选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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控件似乎没有SelectedObjectsSelectedObjectsOverride成员.我正在针对.NET Framework 4.0使用最新版(2.5)的工具包.

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.

@ faztp12的回答使我明白了.对于其他寻求解决方案的人,请按照以下步骤操作:

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

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

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

<xctk:PropertyGrid PropertyValueChanged="PG_PropertyValueChanged" SelectedObject="{Binding SelectedObjects[0]}"  />

  • 监听PropertyGridPropertyValueChanged事件,并使用以下代码将属性值更新为所有选定对象.

  • 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.

    推荐答案

    遇到类似问题时我做了什么,就是我订阅了PropertyValueChanged并让List充满了SelectedObjects.

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

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

    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 :)

    希望对您有所帮助:)

    引用 Xceed文件

    这篇关于WPF PropertyGrid支持多项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆