【问题标题】:How to make container dependent control?如何进行容器依赖控制?
【发布时间】:2014-03-08 05:57:32
【问题描述】:

我正在 C# 应用程序中创建向导控件。所以,我创建了两个类 XWizardControlXWinzardPage 。 XWizardPage 的集合将在 XWizardControl 中创建。用户可以像 TabControl 中的 TabPage 一样,在 XWizardControl 中手动添加 XWizardPage 的数量。 我的问题是开发人员不能直接在任何其他容器或表单中使用该 XWizardPage 控件。 他们不能直接添加到任何容器中。但是他们可以从编辑器创建对象,它不应该显示在我的工具箱中。所以,我很困惑我应该使用什么属性来隐藏 ToolBox 中的 PageControl 以及如何避免用户将该控件添加到任何其他容器控件中。

类声明如下:我不想让你感到困惑。所以,我只是在声明。

//Main Control
public partial class XWizardControl : DevExpress.XtraEditors.XtraUserControl
{
    public XWizardPageCollection Pages
    { get; set; }        
}

//Collection of WizardPage
class XWizardPageCollection : IEnumerable<XWizardPage>, ICollection<XWizardPage>

//Wizard Page
public partial class XWizardPage : DevExpress.XtraEditors.XtraPanel, IComparable<XWizardPage>
{
   //Some Properties
   public XWizardPage()  //Contructor
}

如果您需要更多规格,请告诉我。我将编辑我的问题,在 UPDATE:

中给出详细信息

您也可以在 VB 中提供解决方案。

【问题讨论】:

    标签: c# vb.net winforms user-controls


    【解决方案1】:
    1. 要从工具箱中隐藏控件,请添加ToolboxItemAttribute
    2. 为避免 XWizardPage 成为 XWizardControl 以外的控件的父级(在设计时),您需要创建自定义 ControlDesigner 并覆盖 CanBeParentedTo

    示例

    Public Class XWizardPageDesigner
        Inherits PanelDesigner
    
        Public Overrides Function CanBeParentedTo(ByVal parentDesigner As IDesigner) As Boolean
            Return ((Not parentDesigner Is Nothing) AndAlso TypeOf parentDesigner.Component Is XWizardControl)
        End Function
    
    End Class
    
    <ToolboxItem(False), Designer(GetType(XWizardPageDesigner))> _
    Public Class XWizardPage
        Inherits DevExpress.XtraEditors.XtraPanel
        Implements IComparable(Of XWizardPage)
    
    End Class
    

    更新

    此外,您可能希望为XWizardControl 创建一个自定义ControllCollection 并覆盖add,以便只能添加XWizardPage

    Public Class XWizardControl
        Inherits DevExpress.XtraEditors.XtraUserControl
    
        Protected Overrides Function CreateControlsInstance() As Control.ControlCollection
            Return New XWizardControlCollection(Me)
        End Function
    
        Private Class XWizardControlCollection
            Inherits DevExpress.XtraEditors.XtraUserControl.ControlCollection
    
            Public Sub New(owner As XWizardControl)
                MyBase.New(owner)
            End Sub
    
            Public Overrides Sub Add(ByVal value As Control)
                If (Not TypeOf value Is XWizardPage) Then
                    Throw New ArgumentException()
                End If
                MyBase.Add(value)
            End Sub
    
        End Class
    
    End Class
    

    【讨论】:

    • PenalControl 与设计师类 ControlDesignerPanelDesigner 无关。例如:如果控件的设计器属性设置为PanelDesigner,它将支持在设计时将控件作为该控件的父级。所以如果你有一个文本框,那么你就不想使用这个设计器。顺便说一句,您可以从 ControlDesigner 继承,但是您需要实现 PanelDesigner 的所有功能。
    • 我明白了。现在我创建了另一个由ControlDesigner 继承的类XWizardPageDesigner,并且Designer Attribute 已设置为XWizardPage。现在我的问题已经解决了。我学到了一些新东西。谢谢 Bjørn-Roger Kringsjå。
    • @BjørnRogerKringsjå 我有一个question 和一个bug。你对这些有什么想法吗?
    • 我去看看。
    猜你喜欢
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多