簡體   English   中英

自定義通用用戶控件未出現在工具箱中

[英]Custom Generic UserControl doesn't appear in Toolbox

我需要使用泛型創建自定義用戶控件,因為我有一個數據源類型為TBindingSource

public partial class ABMControl<T> : UserControl
{
    public ABMControl()
    {
        InitializeComponent();
        this.bindingSource.Datasource = typeof(T);
    }
}

在表單設計器中,自定義用戶控件不會出現在工具箱中,因為它是通用的。 解決辦法是什么?

這是工具箱的預期行為。

將控件從工具箱拖放到窗體上時,您是在命令設計器創建該控件的實例。 不能創建的實例GenericControl<T>而不確定T 相反,您需要一個GenericControl<SomeClass>實例。

所以通用控件沒有出現在工具箱中是完全有道理的,因為它在設計器中沒有使用,並且設計器在創建實例時不知道它應該為通用參數使用什么類型。

同樣關於設計器,請考慮這篇文章:從VS2015.1開始的 UserControl 通用基類,Windows 窗體設計器顯示具有通用基類的類,沒有任何問題。 以下類將毫無問題地顯示在設計器中:

public class SomeClassControl:GenericControl<SomeClass>
{
}

對於舊版本的 Visual Studio,請使用鏈接帖子中描述的解決方法:

public class SomeClassControl:SomeClassControlBase
{
}
public class SomeClassControlBase:GenericControl<SomeClass>{}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM