0
votes

I have a PropertyGrid in WPF, I can see that you can set the SelectedObject property of the PropertyGrid to an instance of an object, but I am trying to find a way to edit multiple objects at the same time, like the properties grid in Visual Studio for example. If multiple objects are selected and they share some properties the edit value in the property grid will be applied to every selected object.

So I have

ObservableCollection<SomeObject> ObjectsCollection
ObservableCollection<SomeObject> SelectedObjects

binding the SelectedObject property to the SelectedObjects collection will show each object in a row in the PropertyGrid of DevExpress, but I only want to show the properties, and if one property is edited then it will be applied to all SelectedObjects

Is that possible with DevExpress property grid? I am using the latest version.

1

1 Answers

0
votes

Thats how i did it from within the ViewModel, its working perfectly but not sure if its the best way

private ObservableCollection<Model> items;
            public ObservableCollection<Model> Items
            {
                get
                {
                    return items;
                }
                set
                {
                    SetField(ref items, value, "Items");
                }
            }

            private ObservableCollection<Model> selectedItems;
            public ObservableCollection<Model> SelectedItems
            {
                get
                {
                    return selectedItems;
                }
                set
                {

                    SetField(ref selectedItems, value, "SelectedItems");

                }
            }

            private Model selectedItem;
            public Model SelectedItem
            {
                get
                {
                    return selectedItem;
                }
                set
                {
                    if(selectedItem!=null)selectedItem.PropertyChanged -= ItemChanged;
                    SetField(ref selectedItem, value, "SelectedItem");
                    if(selectedItem!=null)selectedItem.PropertyChanged += ItemChanged;
                }
            }


            private void ItemChanged(object s, PropertyChangedEventArgs e)
            {
                if (selectedItems.Count > 1)
                {
                    if(selectedItems.Contains(selectedItem))
                    SetMultiplePropertyValues<Model>(e.PropertyName, selectedItem.GetType().GetProperty(e.PropertyName).GetValue(s, null));
                }
            }

            private void SetMultiplePropertyValues<T>(string propertyName,object value)
            {
                var p = typeof(T).GetProperty(propertyName);
                foreach (var item in selectedItems)
                {
                    p.SetValue(item, value, null);
                }
            }