Share via


IPropertyValueUIService 介面

定義

為顯示於屬性瀏覽器中的元件屬性提供介面以管理影像、工具提示和事件處理常式。

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

範例

下列程式代碼範例會建立元件,以取得 介面的 IPropertyValueUIService 實例,並將 加入 PropertyValueUIHandler 至服務。 處理程式會為名為 HorizontalMarginVerticalMargin之元件的任何屬性提供 PropertyValueUIItem 物件。 這些屬性的 會 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 實作 (Implementation) 已經修改了 PropertyValueUIItem 物件的全域清單。

RemovePropertyValueUIHandler(PropertyValueUIHandler)

把指定的 PropertyValueUIHandler 從屬性值 UI 服務中移除。

事件

PropertyUIValueItemsChanged

發生於修改 PropertyValueUIItem 物件清單之後。

適用於

另請參閱