如何創建這樣的任務小組? [英] How to Create such a Task Panel?

查看:0
本文介紹了如何創建這樣的任務小組?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在Visual Studio 2008中
如果您創建了一個窗體并在其上放置了控件,
您可以通過"屬性"窗口編輯控件的屬性。

某些控件允許以另一種方式更改其屬性
除"屬性"窗口之外。

如下所示:

似乎所有具有此窗格的控件都具有相同的樣式
這意味著它是由Visual Studio提供的,
而控件的制造者只選擇要包含在其中的項,
如字段和可打開某些窗口的可點擊鏈接。

所以我的問題:
此窗格控件的名稱是什么,
我如何創建一個?

推薦答案

該菜單稱為Smart Tags or Designer Actions,您可以將智能標記添加到您的控件。為此,您需要為控件創建自定義Designer,并在設計器中重寫其ActionLists屬性。

示例

假設我們創建了一個具有某些屬性的控件,并且希望在智能標記窗口中顯示Out控件的以下屬性:

public Color SomeColorProperty { get; set; }
public string[] Items { get; set; }

我們的預期結果是:

MyControl

這里我們使用Designer屬性來裝飾控件以注冊自定義設計器:

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.Design;

[Designer(typeof(MyControlDesigner))]
public partial class MyControl : UserControl
{
    public MyControl()
    {
        InitializeComponent();
    }
    void InitializeComponent() { }
    public Color SomeColorProperty { get; set; }
    public string[] Items { get; set; }
}

MyControlDesigner

這里我們覆蓋ActionLists并返回一個新的DesignerActionListCollection,其中包含我們需要的操作列表項:

public class MyControlDesigner : ControlDesigner
{
    private DesignerActionListCollection actionList;
    public override DesignerActionListCollection ActionLists
    {
        get
        {
            if (actionList == null)
                actionList = new DesignerActionListCollection(new[] {
                    new MyControlActionList(this) });
            return actionList;
        }
    }
}

MyControlActionList

在這里,我們創建獲取/設置控件屬性的屬性。我們還創建了一些方法,這些方法負責顯示某些屬性的自定義編輯器或執行一些操作。然后通過覆蓋GetSortedActionItems

返回措施項列表
public class MyControlActionList : DesignerActionList
{
    ControlDesigner designer;
    MyControl control;
    public MyControlActionList(ControlDesigner designer) : base(designer.Component)
    {
        this.designer = designer;
        control = (MyControl)designer.Control;
    }
    public Color SomeColorProperty
    {
        get { return control.SomeColorProperty;  }
        set {
            TypeDescriptor.GetProperties(
                (object)this.Component)["SomeColorProperty"]
                .SetValue((object)this.Component, (object)value);
        }
    }
    public void EditItems()
    {
        var editorServiceContext = typeof(ControlDesigner).Assembly.GetTypes()
            .Where(x => x.Name == "EditorServiceContext").FirstOrDefault();
        var editValue = editorServiceContext.GetMethod("EditValue",
            System.Reflection.BindingFlags.Static |
            System.Reflection.BindingFlags.Public);
        editValue.Invoke(null, new object[] { designer, this.Component, "Items" });
    }

    public override DesignerActionItemCollection GetSortedActionItems()
    {
        return new DesignerActionItemCollection() {
            new DesignerActionMethodItem(this, "EditItems", "Edit Items",  true),
            new DesignerActionPropertyItem("SomeColorProperty", "Some Color"),
        };
    }
}

有關此主題的更多信息,請查看this MSDN Walkthrough。

下載示例

您可以從以下存儲庫中下載工作示例:

這篇關于如何創建這樣的任務小組?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持IT屋!

查看全文
登錄 關閉
掃碼關注1秒登錄
發送“驗證碼”獲取 | 15天全站免登陸
全免费A级毛片免费看无码播放