2

When you bind a control's property using DataBindings to a datasource, the property grid will show a little purple or black symbol in that property coresponding grid item.

Even when you place your PropertyGrid on the form and set its SelectedObject property to the control with the bound property, that PropertyGrid on the form will show that symbol as well.

But only at design time.

Is there a (simple) way to make the very same PropertyGrid to show this symbol at runtime?

1

1 Answer 1

2

It is handled by Visual Studio designer internal things. But you also can add this feature to PropertyGrid:

enter image description here

You need to implement IPropertyValueUIService and using reflection, assign an instance of the service to the grid entries which should show the glyph. This implementation has a method GetPropertyUIValueItems which can be used to provide that glyph and a tooltip to show near the property label in PropertyGrid. Those values will be used in PaintLabel method of the property grid entry.

Then create an inherited PropertyGrid and override OnSelectedObjectsChanged and OnPropertySortChanged. In those method for each property grid entry item which is presenting a property which is in data bindings collection, set an instance of the implemented IPropertyValueUIService as value of pvSvc private property of the PropertyGrid and attach an event handler which will be called when PropertyGrid requests for additional information about the property. By attaching an event handler using GetPropertyUIValueItems to you can return the tooltip and image which you are going to show in front of the property.

Example

You can download the full example here:

You can find main classes of the implementation as follows.

PropertyValueUIService

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;

namespace PropertyGridDataBindingGlyph
{
    public class PropertyValueUIService : IPropertyValueUIService
    {
        private PropertyValueUIHandler handler;
        private ArrayList items;

        public event EventHandler PropertyUIValueItemsChanged;
        public void NotifyPropertyValueUIItemsChanged()
        {
            PropertyUIValueItemsChanged?.Invoke(this, EventArgs.Empty);
        }
        public void AddPropertyValueUIHandler(PropertyValueUIHandler newHandler)
        {
            handler += newHandler ?? throw new ArgumentNullException("newHandler");
        }
        public PropertyValueUIItem[] GetPropertyUIValueItems(ITypeDescriptorContext context, PropertyDescriptor propDesc)
        {
            if (propDesc == null)
                throw new ArgumentNullException("propDesc");
            if (this.handler == null)
                return new PropertyValueUIItem[0];
            lock (this)
            {
                if (this.items == null)
                    this.items = new ArrayList();
                this.handler(context, propDesc, this.items);
                int count = this.items.Count;
                if (count > 0)
                {
                    PropertyValueUIItem[] propertyValueUiItemArray = new PropertyValueUIItem[count];
                    this.items.CopyTo((Array)propertyValueUiItemArray, 0);
                    this.items.Clear();
                    return propertyValueUiItemArray;
                }
            }
            return null;
        }
        public void RemovePropertyValueUIHandler(PropertyValueUIHandler newHandler)
        {
            handler -= newHandler ?? throw new ArgumentNullException("newHandler");

        }
    }
}

ExPropertyGrid

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace PropertyGridDataBindingGlyph
{
    public class ExPropertyGrid : PropertyGrid
    {
        Bitmap dataBitmap;
        public ExPropertyGrid()
        {
            dataBitmap = new Bitmap(typeof(ControlDesigner).Assembly
                 .GetManifestResourceStream("System.Windows.Forms.Design.BoundProperty.bmp"));
            dataBitmap.MakeTransparent();
        }
        protected override void OnSelectedObjectsChanged(EventArgs e)
        {
            base.OnSelectedObjectsChanged(e);
            this.BeginInvoke(new Action(() => { ShowGlyph(); }));
        }
        protected override void OnPropertySortChanged(EventArgs e)
        {
            base.OnPropertySortChanged(e);
            this.BeginInvoke(new Action(() => { ShowGlyph(); }));
        }
        private void ShowGlyph()
        {
            var grid = this.Controls[2];
            var field = grid.GetType().GetField("allGridEntries",
            System.Reflection.BindingFlags.NonPublic |
            System.Reflection.BindingFlags.Instance | BindingFlags.FlattenHierarchy);
            var value = field.GetValue(grid);
            if (value == null)
                return;
            var entries = (value as IEnumerable).Cast<GridItem>().ToList();
            if (this.SelectedObject is Control)
            {
                ((Control)this.SelectedObject).DataBindings.Cast<Binding>()
                    .ToList().ForEach(binding =>
                    {
                        var item = entries.Where(x => x.PropertyDescriptor?.Name == binding.PropertyName).FirstOrDefault();
                        var pvSvcField = item.GetType().GetField("pvSvc", BindingFlags.NonPublic |
                            BindingFlags.Instance | BindingFlags.FlattenHierarchy);
                        IPropertyValueUIService pvSvc = new PropertyValueUIService();
                        pvSvc.AddPropertyValueUIHandler((context, propDesc, valueUIItemList) =>
                        {
                            valueUIItemList.Add(new PropertyValueUIItem(dataBitmap, (ctx, desc, invokedItem) => { }, GetToolTip(binding)));
                        });
                        pvSvcField.SetValue(item, pvSvc);
                    });
            }
        }
        private static string GetToolTip(Binding binding)
        {
            var value = "";
            if (binding.DataSource is ITypedList)
                value = ((ITypedList)binding.DataSource).GetListName(new PropertyDescriptor[] { });
            else if (binding.DataSource is Control)
                value = ((Control)binding.DataSource).Name;
            else if (binding.DataSource is Component)
                value = ((Component)binding.DataSource).Site?.Name;

            if (string.IsNullOrEmpty(value))
                value = "(List)";
            return value + " - " + binding.BindingMemberInfo.BindingMember;
        }
    }
}
1
  • After cloning/downloading the example project from git, just build the project and run it. At run-time you will what I shared in screenshot. Jan 16, 2018 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.