[英]How can I use the Xamarin.Forms.Setter class in my own ContentView (custom control)?
[英]How can I use a custom control class that inherits from a built-in control on my main form?
我有一個mydatagridview類,它從內置的DataGridView
控件繼承,如下所示:
public class mydatagridview : DataGridView
{
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.ProcessTabKey(e.KeyData);
return true;
}
return base.ProcessDataGridViewKey(e);
}
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter)
{
this.ProcessTabKey(keyData);
return true;
}
return base.ProcessDialogKey(keyData);
}
}
現在,我想在我的主課中使用它:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
我想將myDatagridview與Datagridview1一起使用:公共局部類Form1:Form
我怎樣才能做到這一點?
您需要創建自定義控件類的實例,然后將該實例添加到窗體的Controls
集合中。 例如:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Create an instance of your custom control
mydatagridview myDGV = new mydatagridview();
// Add that instance to your form's Controls collection
this.Controls.Add(myDGV);
}
}
當然,您也可以從Designer中執行相同的操作。 它將自動在InitializeComponent()
方法中插入與上面顯示的代碼非常相似的代碼。
如果重建項目后自定義控件未自動顯示在工具箱中,請確保已啟用工具箱自動填充 :
將“ AutoToolboxPopulate”屬性設置為True。
如果我理解正確,但不確定是否可以這樣做,則可以像使用任何其他類型一樣使用它:
mydatagridview mydatagrid = new mydatagridview();
this.Controls.Add(mydatagrid);
在已經給出的答案旁邊,應該可以將控件從工具箱拖放到表單中。
如果您創建用戶控件或自定義控件,並生成您的項目,則該控件應顯示在工具箱中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.