【问题标题】:type-bound custom ComboBox deriving from ComboBox从 ComboBox 派生的类型绑定自定义 ComboBox
【发布时间】:2010-09-10 05:56:25
【问题描述】:

我应该通过在我的 WinForms 应用程序中从 ComboBox 派生一个类来创建自定义 ComboBox。我以前从未这样做过,也无法从 Google 中找到很多好的示例。

我需要派生一个自定义组合框,以便我可以将自定义组合框类型绑定到特定对象。

你能指出我正确的方向吗?

这是我目前所拥有的。

CustomComboBox.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MAPClient {
    class MAPCodeComboBox : ComboBox {

    }
}

我有一些具体问题:

  1. 我需要重写哪些方法?
  2. 如何在我的 VS2010 设计器模式下使用它?

【问题讨论】:

  • “我需要重写哪些方法?” - 好吧....它需要做些什么不同的事情?另外,你应该澄清这是否是winforms、asp.net、wpf、silverlight等......
  • 如果这是您第一次使用自定义控件完成任何工作,那么您也许应该阅读一下它?见Developing Custom Windows Forms Controls with the .NET Framework
  • @John Saunders:感谢您的链接 :)
  • 使其成为强类型绑定的工作比您预期的要多(正确包装和隐藏DataSourceItems) - 真的值得吗?
  • @Marc Gravell:“真的值得吗?” - 不。但是,我必须这样做!请不要问为什么!!

标签: c# winforms combobox custom-controls type-bounds


【解决方案1】:

好的,最后我有以下自定义类型绑定的 ComboBox。如果我做错了什么,请告诉我。

MAPComboBox.cs

using System.Collections.Generic;
using System.Windows.Forms;

namespace MAPClient {
    class MAPComboBox : ComboBox {
        private MAPCodeObjectCollection MAPCodeItemCollection = null;

        new public MAPCodeObjectCollection Items {
            // override
        }

        new public List<MAPCode> DataSource {
            // override
        }

        public MAPCodeComboBox() { }
    }
}

MAPCodeObjectCollection.cs

using System.Windows.Forms;

namespace MAPClient {
    class MAPCodeObjectCollection : ComboBox.ObjectCollection {
        public MAPCodeObjectCollection(ComboBox owner) : base(owner) { }

        new public int Add(object item) {
            // override
        }

        new public void Insert(int index, object item) {
            // override
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    相关资源
    最近更新 更多