silverlight - How to add an item to a ComboBox programmatically

Add ComboBox Item programmatically in silverlight
ComboBox is a silverlight selection control. combobox combines a non-editable textbox and a drop-down containing a list box that allow us to select an item from list. combobox is an ItemsControl which means combobox has an associated item container. ComboBoxItem represent a selectable item contained in a combobox control.

combobox Items property get the collection used to generate the content of the control. if we add or remove an item from this Items collection then it will also add or remove the specified item from combobox itself. we can add an item to combobox programmatically at run time by using Items.Add() method. this Add() method add an item to combobox items collection.

to do this first we need to create a ComboBoxItem. after creating the ComboBoxItem we will add it to combobox items collection by Items.Add() method. finally combobox will show the newly added item.

the following silverlight c# example code demonstrate us how can we add an item to combobox programmatically at run time in a silverlight application.
MainPage.xaml

<UserControl x:Class="SilverlightApps.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="600" 
    d:DesignWidth="800" 
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:Label 
            HorizontalAlignment="Left" 
            Margin="12,21,0,0" 
            Name="label1" 
            VerticalAlignment="Top" 
            Width="750"
            Content="Silverlight - how to add ComboBox Item programmatically"
            FontSize="25"
            FontStyle="Italic"
            Foreground="MidnightBlue"
            />
        <ComboBox 
            Height="23" 
            HorizontalAlignment="Left" 
            Margin="234,117,0,0" 
            Name="comboBox1" 
            VerticalAlignment="Top" 
            Width="120" 
            >
            <ComboBoxItem Content="India"></ComboBoxItem>
            <ComboBoxItem Content="Bangladesh" IsSelected="True"></ComboBoxItem>
            <ComboBoxItem Content="USA"></ComboBoxItem>
        </ComboBox>

        <Button 
            Content="Click Here To Add A New ComboBox Item" 
            Height="23" 
            HorizontalAlignment="Left" 
            Margin="182,258,0,0" 
            Name="button1" 
            VerticalAlignment="Top" 
            Click="button1_Click"
            />
    </Grid>
</UserControl>
MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApps
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ComboBoxItem cbi = new ComboBoxItem();
            cbi.Content = "Canada";
            comboBox1.Items.Add(cbi);
        }
    }
}
More silverlight examples