IPropertyValueUIService 接口

定义

提供一个界面,用于管理属性浏览器中显示的组件的属性的图像、工具提示和事件处理程序。

public interface class IPropertyValueUIService
public interface IPropertyValueUIService
type IPropertyValueUIService = interface
Public Interface IPropertyValueUIService

示例

下面的代码示例创建一个组件,该组件获取接口的 IPropertyValueUIService 实例并向服务添加一个 PropertyValueUIHandler 实例。 处理程序为名为PropertyValueUIItemHorizontalMargin. 的组件的任何属性提供对象VerticalMargin。 这些 PropertyValueUIItem 属性提供图像、工具提示和事件处理程序,用于在单击该属性的图像时显示消息框。 当网格显示 PropertyGrid 组件的这些属性时,图像和工具提示会显示。

using System.Collections;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;

namespace PropertyValueUIServiceExample
{
    // This component obtains the IPropertyValueUIService and adds a
    // PropertyValueUIHandler that provides PropertyValueUIItem objects,
    // which provide an image, ToolTip, and invoke event handler to
    // any properties named HorizontalMargin and VerticalMargin, 
    // such as the example integer properties on this component.    
    public class PropertyUIComponent : System.ComponentModel.Component
    {
        // Example property for which to provide a PropertyValueUIItem.
        public int HorizontalMargin { get; set; }

        // Example property for which to provide a PropertyValueUIItem.
        public int VerticalMargin { get; set; }

        // Field storing the value of the VerticalMargin property.
        private int vMargin;

        // Constructor.
        public PropertyUIComponent(System.ComponentModel.IContainer container)
        {
            if (container != null)
                container.Add(this);
            HorizontalMargin = 0;
            VerticalMargin = 0;
        }

        // Default component constructor that specifies no container.
        public PropertyUIComponent() : this(null)
        { }

        // PropertyValueUIHandler delegate that provides PropertyValueUIItem
        // objects to any properties named HorizontalMargin or VerticalMargin.
        private void marginPropertyValueUIHandler(
            System.ComponentModel.ITypeDescriptorContext context,
            System.ComponentModel.PropertyDescriptor propDesc,
            ArrayList itemList)
        {
            // A PropertyValueUIHandler added to the IPropertyValueUIService
            // is queried once for each property of a component and passed
            // a PropertyDescriptor that represents the characteristics of 
            // the property when the Properties window is set to a new 
            // component. A PropertyValueUIHandler can determine whether 
            // to add a PropertyValueUIItem for the object to its ValueUIItem 
            // list depending on the values of the PropertyDescriptor.
            if (propDesc.DisplayName.Equals("HorizontalMargin"))
            {
                Image img = Image.FromFile("SampImag.jpg");
                itemList.Add(new PropertyValueUIItem(img, new PropertyValueUIItemInvokeHandler(this.marginInvoke), "Test ToolTip"));
            }
            if (propDesc.DisplayName.Equals("VerticalMargin"))
            {
                Image img = Image.FromFile("SampImag.jpg");
                img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                itemList.Add(new PropertyValueUIItem(img, new PropertyValueUIItemInvokeHandler(this.marginInvoke), "Test ToolTip"));
            }
        }

        // Invoke handler associated with the PropertyValueUIItem objects 
        // provided by the marginPropertyValueUIHandler.
        private void marginInvoke(System.ComponentModel.ITypeDescriptorContext context, System.ComponentModel.PropertyDescriptor propDesc, PropertyValueUIItem item)
        {
            MessageBox.Show("Test invoke message box");
        }

        // Component.Site override to add the marginPropertyValueUIHandler
        // when the component is sited, and to remove it when the site is 
        // set to null.
        public override System.ComponentModel.ISite Site
        {
            get
            {
                return base.Site;
            }
            set
            {
                if (value != null)
                {
                    base.Site = value;
                    IPropertyValueUIService uiService = (IPropertyValueUIService)this.GetService(typeof(IPropertyValueUIService));
                    if (uiService != null)
                        uiService.AddPropertyValueUIHandler(new PropertyValueUIHandler(this.marginPropertyValueUIHandler));
                }
                else
                {
                    IPropertyValueUIService uiService = (IPropertyValueUIService)this.GetService(typeof(IPropertyValueUIService));
                    if (uiService != null)
                        uiService.RemovePropertyValueUIHandler(new PropertyValueUIHandler(this.marginPropertyValueUIHandler));
                    base.Site = value;
                }
            }
        }
    }
}

注解

组件可以使用 IPropertyValueUIService 接口为组件的任何属性提供 PropertyValueUIItem 对象。 PropertyValueUIItem与属性关联的事件可以提供图像、工具提示和事件处理程序,该事件在单击与该属性关联的图像时引发。

IPropertyValueUIService 接口提供用于向内部列表或从内部列表中添加、删除和检索 PropertyValueUIHandler 委托的方法。 当组件的属性显示在属性浏览器中时,列表中的每个 PropertyValueUIHandler 属性都有机会为组件的每个属性提供一个 PropertyValueUIItem

当属性浏览器设置为显示对象的属性时,它将针对组件的每个属性调用 GetPropertyUIValueItems 此接口的方法,并传递一个 PropertyDescriptor 表示该属性的属性。 该方法 GetPropertyUIValueItems 调用已添加到服务的每个 PropertyValueUIHandler 方法。 每个PropertyValueUIHandler参数都可以将一个PropertyValueUIItemArrayList添加到参数中valueUIItemList传递的参数,以提供由PropertyDescriptor参数中propDesc传递的属性的 UI 项。

可以 PropertyValueUIItem 包含一个图像,该图像显示在属性名称旁边、工具提示字符串以及双击与属性关联的图像时要调用的事件处理程序。

方法

名称 说明
AddPropertyValueUIHandler(PropertyValueUIHandler)

将指定的 PropertyValueUIHandler 值添加到此服务。

GetPropertyUIValueItems(ITypeDescriptorContext, PropertyDescriptor)

PropertyValueUIItem获取与指定上下文和属性描述符特征匹配的对象。

NotifyPropertyValueUIItemsChanged()

通知 IPropertyValueUIService 实现已修改对象的全局列表 PropertyValueUIItem

RemovePropertyValueUIHandler(PropertyValueUIHandler)

从属性值 UI 服务中删除指定的 PropertyValueUIHandler 值。

活动

名称 说明
PropertyUIValueItemsChanged

修改对象列表 PropertyValueUIItem 时发生。

适用于

另请参阅