c# - 使 Propertygrid 在运行时显示绑定(bind)的属性符号

标签 c# winforms data-binding windows-forms-designer propertygrid

当您使用 DataBindings 将控件的属性绑定(bind)到数据源时,属性网格将在该属性对应的网格项中显示一个紫色或黑色的小符号。

即使将 PropertyGrid 放在窗体上并将其 SelectedObject 属性设置为具有绑定(bind)属性的控件,窗体上的 PropertyGrid 也会显示该符号。

但仅限于设计时。

是否有一种(简单)方法可以使完全相同的 PropertyGrid 在运行时显示此符号?

最佳答案

它是由 Visual Studio 设计器内部处理的。但您也可以将此功能添加到 PropertyGrid:

enter image description here

您需要实现IPropertyValueUIService并使用反射,将服务的实例分配给应显示字形的网格条目。此实现有一个方法 GetPropertyUIValueItems,可用于提供该字形和工具提示以显示在 PropertyGrid 中的属性标签附近。这些值将在属性网格条目的 PaintLabel 方法中使用。

然后创建继承的 PropertyGrid 并覆盖 OnSelectedObjectsChangedOnPropertySortChanged。在这些方法中,对于呈现数据绑定(bind)集合中的属性的每个属性网格条目项,将已实现的 IPropertyValueUIService 的实例设置为该属性的 pvSvc 私有(private)属性的值。 PropertyGrid 并附加一个事件处理程序,当 PropertyGrid 请求有关属性的其他信息时将调用该事件处理程序。通过使用 GetPropertyUIValueItems 附加事件处理程序,您可以返回要在属性前面显示的工具提示和图像。

示例

您可以在此处下载完整示例:

您可以找到实现的主要类如下。

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;
        }
    }
}

关于c# - 使 Propertygrid 在运行时显示绑定(bind)的属性符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48272629/

相关文章:

c# - 添加服务引用会生成空的 reference.cs 和空的 config.svcinfo 文件

c# - 将 C# 日期时间值与 SQL Server Compact 4 日期时间进行比较

c# - 有什么方法可以控制 TableLayoutPanel 中单元格边框的粗细?

vb.net - 调整窗口大小时如何调整表单元素的大小?

Grails 数据绑定(bind)字段排除

c# - WPF 嵌套数据绑定(bind)到控件 - 为什么它不起作用

c# - Marshal.AllocHGlobal VS Marshal.AllocCoTaskMem,Marshal.SizeOf VS sizeof()

c# - Winform 数据源更新

c# - WPF:如何在 IValueConverter 抛出的 UI 中显示错误消息?

c# - 您可以从系统中获取默认文件夹的打开/关闭图标而没有实际路径吗?