How to make property readonly when multiple objects are selected?

Grids for WPF Forum

Posted 9 years ago by Gareth Parris - Software Developer, McLaren
Version: 15.1.0621
Avatar

I have a property grid that can have single items or multiple items selected.

When single items are selected the name property can be edited and this works okay.

When multiple items are selected I want the name property to become readonly so it cannot be changed?

Is this possible?

Comments (5)

Answer - Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Gareth,

Check out the "Selective Read-only" QuickStart since that shows how you can dynamically make properties read-only.  Your scenario is a bit different because it needs to be based on multiple objects.  I believe that if you have a custom data factory, you can override the CreateMergedPropertyDataAccessor method and provide a customized MergedPropertyDataAccessor data accessor there that has the Name property as read-only, similar to the QuickStart.


Actipro Software Support

Posted 9 years ago by Robert Dyer
Avatar

I am going to be facing this same exact problem shortly, and I think a simple MVVM answer to this would be to set a two way binding on the IsReadOnly dependency property to a boolean property on your view model.. This boolean property could return false when multiple objects are selected on an associated ListBox and true otherwise. There could of course be other reasons to set the property to false regardless of whether one or multiple objects are selected. Our property grid subscribes to a PRISM event aggregator event every time the selection on the ListBox changes, so we always know what objects are selected in the list box which happens to be in another view model in our case.

Am I on the right track here? Any feedback from readers is welcome.

[Modified 9 years ago]

Posted 9 years ago by Gareth Parris - Software Developer, McLaren
Avatar

The above solution was correct. Our issue was resolved with the following two classes.

    internal sealed class ReadOnlyMergedPropertyDataAccessorFactory : TypeDescriptorFactory
    {
        protected override IPropertyDataAccessor CreateMergedPropertyDataAccessor(
            IPropertyDataAccessor parent,
            IList<IPropertyDataAccessor> propertyDataAccessors)
        {
            return new ReadOnlyMergedPropertyDataAccessor(parent, propertyDataAccessors);
        }
    }

 

    internal sealed class ReadOnlyMergedPropertyDataAccessor : MergedPropertyDataAccessor
    {
        private bool isReadOnly;

        public ReadOnlyMergedPropertyDataAccessor(IList<IPropertyDataAccessor> propertyDataAccessors)
            : base(propertyDataAccessors)
        {
            this.SetReadOnly(propertyDataAccessors);
        }

        public ReadOnlyMergedPropertyDataAccessor(IPropertyDataAccessor parent, IList<IPropertyDataAccessor> propertyDataAccessors)
            : base(parent, propertyDataAccessors)
        {
            this.SetReadOnly(propertyDataAccessors);
        }

        public override bool IsReadOnly
        {
            get { return this.isReadOnly; }
        }

        private void SetReadOnly(IList<IPropertyDataAccessor> propertyDataAccessors)
        {
            switch (propertyDataAccessors[0].ValueName)
            {
                case "DisplayName":
                case "Name":
                {
                    this.isReadOnly = true;
                    break;
                }
            }
        }
    }
Posted 9 years ago by Robert Dyer
Avatar

Thanks for your response. The approach I described above also works fine, and the implementation is simpler from my point of view, but it is great to understand all avenues for approaching this problem. So I will also keep your posted solution in mind.

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Robert,

However you do it, the property editor templates all bind to the IsReadOnly property on the related data accessor that is provided by the data factory.  You can update that data accessor IsReadOnly property value as needed and make sure you trigger the PropertyChanged notification event when you do so that the bindings in the templates know to pick up the change.


Actipro Software Support

The latest build of this product (v24.1.2) was released 2 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.