2013-02-28 70 views
1

我在應用程序中看到這一點,只是想知道這是否可以使用C#的WinForms來完成。 我在自定義PropertyGrid控件沒有經驗,我會高度讚賞,如果你能好心幫助我的代碼。提前致謝。如何將ListBox合併到PropertyGridControl中?

Sceenshot:

enter image description here

+2

IIRC,你需要實現一個'System.Drawing.Design.UITypeEditor'。 – leppie 2013-02-28 09:02:25

回答

2

有幾個步驟來實現這一目標。

1.定義枚舉爲你的價值觀。

public enum TypeCodes 
{ 
    Baking, 
    Barbecuing, 
    Blanching, 
    Blind_Baking, 
    Boiling, 
    Broiling, 
    Deep_Frying, 
} 

2.類型爲List的屬性創建類。

using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing.Design; 

namespace PropertyGrid_ListBox_Test 
{ 
    class TestCase 
    { 
     public TestCase() 
     { 
      Manufacturer = new List<TypeCodes>(); 
     } 

     [DisplayName("Manufacturer Preparation Type Code")] 
     public List<TypeCodes> Manufacturer { get; set; } 
    } 
} 

3.創建用戶控件 - TypeCodeSelectionControl將被用於編輯此屬性。

UserControl to edit property value

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

namespace PropertyGrid_ListBox_Test 
{ 
    public partial class TypeCodeSelectionControl : UserControl 
    { 
     public List<TypeCodes> List { get; set; } 
     private IWindowsFormsEditorService editorService; 

     public TypeCodeSelectionControl() 
     { 
      InitializeComponent(); 
     } 

     private void PrepareListBoxes() 
     { 
      foreach (string myType in Enum.GetNames(typeof(TypeCodes))) 
       listBox1.Items.Add(myType); 

      foreach (TypeCodes type_code in this.List) 
      { 
       string enum_value = Enum.GetName(typeof(TypeCodes), type_code); 
       listBox1.Items.Remove(enum_value); 
       listBox2.Items.Add(enum_value); 
      } 
     } 

     public TypeCodeSelectionControl(List<TypeCodes> list, IWindowsFormsEditorService editorService) 
     { 
      InitializeComponent(); 

      this.List = list; 
      this.editorService = editorService; 

      PrepareListBoxes(); 
     } 

     private void buttonALL_Click(object sender, EventArgs e) 
     { 
      // 
      // Add all items. 
      // 
      foreach (object obj in listBox1.Items) 
      { 
       listBox2.Items.Add(obj); 
      } 
      listBox1.Items.Clear(); 
     } 

     private void buttonAdd_Click(object sender, EventArgs e) 
     { 
      // 
      // Add selected item. 
      // 
      string item_to_add = (string)listBox1.SelectedItem; 
      listBox1.Items.Remove(item_to_add); 
      listBox2.Items.Add(item_to_add); 
     } 

     private void buttonRemove_Click(object sender, EventArgs e) 
     { 
      // 
      // Remove selected item. 
      // 
      string item_to_remove = (string)listBox2.SelectedItem; 
      listBox2.Items.Remove(item_to_remove); 
      listBox1.Items.Add(item_to_remove); 
     } 

     private void buttonNone_Click(object sender, EventArgs e) 
     { 
      // 
      // Remove all items. 
      // 
      foreach (object obj in listBox2.Items) 
      { 
       listBox1.Items.Add(obj); 
      } 
      listBox2.Items.Clear(); 
     } 

     private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      // 
      // Enable "Add" button only if any of items is selected in the left listbox. 
      // 
      buttonAdd.Enabled = listBox1.SelectedIndex != -1; 
     } 

     private void listBox2_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      // 
      // Enable "Remove" button only if any of items is selected in the right listbox. 
      // 
      buttonRemove.Enabled = listBox2.SelectedIndex != -1; 
     } 

     protected override void OnVisibleChanged(EventArgs e) 
     { 
      base.OnVisibleChanged(e); 

      // 
      // This occures when dropdown UI editor is hiding. 
      // 
      if (this.Visible == false) 
      { 
       // 
       // Clear previously selected items. 
       // 
       if (this.List == null) 
       { 
        this.List = new List<TypeCodes>(); 
       } 
       else 
       { 
        this.List.Clear(); 
       } 

       // 
       // Fill the list with currently selected items. 
       // 
       foreach (string obj in listBox2.Items) 
       { 
        this.List.Add((TypeCodes)Enum.Parse(typeof(TypeCodes), obj)); 
       } 
       editorService.CloseDropDown(); 
      } 
     } 
    } 
} 

4.創建UITypeEditor的 - 將處理先前創建的用戶控件TypeCodeEditor。

5.創建TypeConverter,才能正確顯示我們的財產價值。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 

namespace PropertyGrid_ListBox_Test 
{ 
    class TypeCodeTypeConverter : TypeConverter 
    { 
     public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
     { 
      if (sourceType == typeof(string)) 
      { 
       return true; 
      } 
      return base.CanConvertFrom(context, sourceType); 
     } 

     public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) 
     { 
      if (value is string) 
      { 
       List<TypeCodes> list = new List<TypeCodes>(); 
       string[] values = ((string)value).Split(new char[] { ';' }); 
       foreach (string v in values) 
       { 
        list.Add((TypeCodes)Enum.Parse(typeof(TypeCodes), v)); 
       } 

       return list; 
      } 
      return base.ConvertFrom(context, culture, value); 
     } 

     public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
     { 
      if (destinationType == typeof(string)) 
      { 
       return true; 
      } 
      return base.CanConvertTo(context, destinationType); 
     } 

     public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) 
     { 
      if (destinationType == typeof(string)) 
      { 
       List<TypeCodes> list = (List<TypeCodes>)value; 
       string result = ""; 
       foreach (TypeCodes type_code in list) 
       { 
        result += Enum.GetName(typeof(TypeCodes), type_code) + "; "; 
       } 
       return result; 
      } 
      return base.ConvertTo(context, culture, value, destinationType); 
     } 
    } 
} 

6.添加適當的屬性的屬性,因此它必將成爲可編輯與我們的下拉菜單自定義編輯器。

using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing.Design; 

namespace PropertyGrid_ListBox_Test 
{ 
    class TestCase 
    { 
     public TestCase() 
     { 
      Manufacturer = new List<TypeCodes>(); 
     } 

     [DisplayName("Manufacturer Preparation Type Code")] 
     [Editor(typeof(TypeCodeEditor), typeof(UITypeEditor))] 
     [TypeConverter(typeof(TypeCodeTypeConverter))] 
     public List<TypeCodes> Manufacturer { get; set; } 
    } 
} 

7.享受成果:)。 enter image description here

我希望這段代碼能夠幫助您繼續編寫自己的自定義編輯器。

完整的示例,請訪問:https://github.com/virious/PropertyGrid_CustomDropDownEditor

+1

優秀的答案!謝謝,比爾 – BillW 2013-10-03 03:15:46

+0

我很高興我能幫上忙。我認爲我的答案應該被標記爲這個問題的解決方案,因爲沒有人用任何解決這個問題的替代方法來回答。 – virious 2013-10-03 09:14:25

+0

@BillW您能否將我的回答標記爲您的問題的答案?我認爲這是你正在尋找的東西:)。 – virious 2014-05-22 20:44:57

相關問題