今日需要弄UI和對象綁定,找到了魏瓊東這位牛人的博客,在裏面吸取了經驗,自己做一個簡單版的就好了,但問題就來了,不知道怎麼實現以下標記的效果。
聯想到combobox中的items也是這樣的表現,所以就看了一下combobx中Items的源碼 。
以下是combobox的Items的源碼
/// <summary>
/// 獲取一個對象,該對象表示該 <see cref="T:System.Windows.Forms.ComboBox"/> 中所包含項的集合。
///
/// </summary>
///
/// <returns>
/// 表示 <see cref="T:System.Windows.Forms.ComboBox"/> 中的項的 <see cref="T:System.Windows.Forms.ComboBox.ObjectCollection"/>。
///
/// </returns>
/// <filterpriority>1</filterpriority>
[Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof (UITypeEditor))]
[SRDescription("ComboBoxItemsDescr")]
[MergableProperty(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Localizable(true)]
[SRCategory("CatData")]
public ComboBox.ObjectCollection Items
{
get
{
if (this.itemsCollection == null)
this.itemsCollection = new ComboBox.ObjectCollection(this);
return this.itemsCollection;
}
}
在這裏Items上有很多attribute從字面上的意思看出只有第一個attribute(Editor)能起到這個作用,於是就MSDN上找到了這個屬性(MSDN鏈接)
重點的內容在備註裏面,在備註裏叫我們override兩個函數 EditValue和 GetEditStyle,詳細的說面在上面的msdn鏈接中有。在EditValue中有一個object 的返回值,一開始並不明白這個是有什麼用的,後來又點擊進入了EditValue中才知道,EditValue的兩個函數簽名中都有value,是和返回的object是同一樣東西來的。
在這裏也提供一個幫助我理解的博客(內附工程)點擊打開鏈接,這個工程我是運行不了,因爲缺少了一個dll庫,但可以看看裏面的源碼幫助理解。