Basic Library for WPF and Silverlight | ComponentOne
WPF and Silverlight Edition Basic Library / Combo Box / ComboBox Features / ComboBox Items / Adding Items
In This Topic
    Adding Items
    In This Topic

    There are several ways to add items to the C1ComboBox control:

    1. To add items to the C1ComboBox control, add the following XAML markup between the <c1:C1ComboBox> and </c1:C1ComboBox> tags:
      XAML
      Copy Code
      <c1:C1ComboBoxItem Height="25" Content="C1ComboBoxItem"/>
      <c1:C1ComboBoxItem Height="25" Content="C1ComboBoxItem"/>
      <c1:C1ComboBoxItem Height="25" Content="C1ComboBoxItem"/>
      <c1:C1ComboBoxItem Height="25" Content="C1ComboBoxItem"/>
      
    2. Run the program.
    3. Click the drop-down arrow and observe that four items appear in the drop-down list.
    1. Add x:Name="C1ComboBox1" to the <c1:C1ComboBox> tag so that the object will have a unique identifier for you to call in code.
    2. Open the MainPage.xaml.cs or the MainWindow.xaml.cs page.
    3. Import the following namespace into your project in Code View:
    C#
    Copy Code
    using C1.WPF;
    

    Visual Basic
    Copy Code
    Imports C1.WPF
    
    1. Add the following code beneath the InitializeComponent() method:
    C#
    Copy Code
    c1ComboBox1.Items.Add(new C1ComboBoxItem() { Content = "C1ComboBoxItem1" });
    c1ComboBox1.Items.Add(new C1ComboBoxItem() { Content = "C1ComboBoxItem2" });
    c1ComboBox1.Items.Add(new C1ComboBoxItem() { Content = "C1ComboBoxItem3" });
    c1ComboBox1.Items.Add(new C1ComboBoxItem() { Content = "C1ComboBoxItem4" });
    

    Visual Basic
    Copy Code
    C1ComboBox1.Items.Add(New C1ComboBoxItem() With {.Content = "C1ComboBoxItem1"})
    C1ComboBox1.Items.Add(New C1ComboBoxItem() With {.Content = "C1ComboBoxItem2"})
    C1ComboBox1.Items.Add(New C1ComboBoxItem() With {.Content = "C1ComboBoxItem3"})
    C1ComboBox1.Items.Add(New C1ComboBoxItem() With {.Content = "C1ComboBoxItem4"})
    
    1. Run the program.
    1. Add x:Name="C1ComboBox1" to the <c1:C1ComboBox> tag so that the object will have a unique identifier for you to call in code.
    2. Open the MainPage.xaml.cs or the MainWindow.xaml.cs page.
    3. Import the following namespace into your project in Code View:
    C#
    Copy Code
    Using C1.Silverlight;
    

    Visual Basic
    Copy Code
    Imports C1.Silverlight
    
    1. Add the following code beneath the InitializeComponent() method:
    C#
    Copy Code
    C1ComboBox1.Items.Add(new C1ComboBoxItem() { Content = "C1ComboBoxItem1" });
    C1ComboBox1.Items.Add(new C1ComboBoxItem() { Content = "C1ComboBoxItem2" });
    C1ComboBox1.Items.Add(new C1ComboBoxItem() { Content = "C1ComboBoxItem3" });
    C1ComboBox1.Items.Add(new C1ComboBoxItem() { Content = "C1ComboBoxItem4" });
    

    Visual Basic
    Copy Code
    C1ComboBox1.Items.Add(New C1ComboBoxItem() With {.Content = "C1ComboBoxItem1"})
    C1ComboBox1.Items.Add(New C1ComboBoxItem() With {.Content = "C1ComboBoxItem2"})
    C1ComboBox1.Items.Add(New C1ComboBoxItem() With {.Content = "C1ComboBoxItem3"})
    C1ComboBox1.Items.Add(New C1ComboBoxItem() With {.Content = "C1ComboBoxItem4"})
    
    1. Run the program.

    Complete the following steps:

    1. Add x:Name="C1ComboBox1" to the <c1:C1ComboBox> tag so that the object will have a unique identifier for you to call in code.
    2. Open the MainWindow.Xaml.cs page or the MainPage.xaml.cs page (or the .vb page if using Visual Basic).
    3. Import the following namespace into the project:
    Visual Basic
    Copy Code
    Imports System.Collections.Generic
    

    C#
    Copy Code
    using System.Collections.Generic;
    
    1. Create your list by adding the following code beneath the InitializeComponent() method:
    Visual Basic
    Copy Code
    Dim dropDownList As New List(Of String)()
    dropDownList.Add("C1ComboBoxItem1")
    dropDownList.Add("C1ComboBoxItem2")
    dropDownList.Add("C1ComboBoxItem3")
    dropDownList.Add("C1ComboBoxItem4")
    

    C#
    Copy Code
    List<string> dropDownList = new List<string>();
    dropDownList.Add("C1ComboBoxItem1");
    dropDownList.Add("C1ComboBoxItem2");
    dropDownList.Add("C1ComboBoxItem3");
    dropDownList.Add("C1ComboBoxItem4");
    
    1. Add the list to the combo box by setting the ItemsSource property:
    Visual Basic
    Copy Code
    C1ComboBox1.ItemsSource = dropDownList
    

    C#
    Copy Code
    c1ComboBox1.ItemsSource = dropDownList;
    

    1. Run the program.

    Complete the following steps:

    1. In the Properties window, click the Items ellipsis button  to open the Collection Editor: Items dialog box.
    Collection Editor: Items

    Collection Editor: Items

    1. Click Add to add a C1ComboBoxItem to the C1ComboBox control.