Click here to Skip to main content
Click here to Skip to main content

Add checkbox inside combobox in Silverlight

By , 28 Nov 2012
 

Introduction

This article helps you to create a combobox with a checkboxlist in Silverlight. In this article I use DependencyProperty, INotifyPropertyChanged, and ItemTemplate for checkbox check/uncheck.

The control looks like:

Checkbox inside Combobox

Background 

In this article I used DependencyProperty for changing the checkbox value in the ComboBox control. A DependencyProperty is a static method that changes the value of an instanced object property. Also I used INotifyPropertyChanged for changing property value in the custom user control.

Anyone can use this control in his/her Silverlight project. Also ItemTemplate is particularly useful when you bind the ItemsSource to data, it binds to a checkbox in a combobox. In this example, when the user checks the checkbox, time is updated in the combobox selected value and also when unchecking any checkbox, the time reflects in the selected value.

In this example, I create a Model class which contains NotifyProperty and returns the list for binding it in the control.

In this example I also use ItemTemplate for displaying a checkbox inside a combobox. When page loads that time assigns the result to an ItemsSource. Also I add a PropertyChanged event for notifying when checkbox is checked or unchecked. When the PropertyChanged event is called, the time binds the selected checkbox value to the combobox selected value.

My MainPage.xaml looks like:

<grid removed="White" x:name="LayoutRoot">
    <stackpanel margin="10">
        <combobox horizontalalignment="Left" width="200" 
              height="25" name="cmbDepartment">
            <combobox.itemtemplate>
                <datatemplate>
                    <checkbox ischecked="{Binding IsChecked,Mode=TwoWay}" 
                       content="{Binding DepartmentName}">
                </checkbox></datatemplate>
            </combobox.itemtemplate>
        </combobox>
        <textblock margin="5,-20,0,0" horizontalalignment="Left" 
           width="Auto" height="20" x:name="tbDepartment">
    </textblock></stackpanel>
</grid>

My MainPage.xaml.cs looks like:

public partial class MainPage : UserControl
{

    public MainPage()
    {
        InitializeComponent();
        ObservableCollection<department> objDeploymentList = new ObservableCollection<department>();
        cmbDepartment.SelectionChanged += new SelectionChangedEventHandler(cmbDepartment_SelectionChanged);
        DepartmentList obj = new DepartmentList();
        objDeploymentList = obj.LoadDepartmentList();
        cmbDepartment.ItemsSource = objDeploymentList;
        foreach (Department item in objDeploymentList)
        {
            item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
        }
        if (cmbDepartment.ItemsSource != null)
        {
            SetString();
        }
    }

    void cmbDepartment_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        cmbDepartment.SelectedItem = null;
    }

    void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "IsChecked")
        {
            SetString();
        }
    }
    //Function for Bind Selected Value in Combobox
    public void SetString()
    {
        string str = "";
        foreach (Department item in cmbDepartment.ItemsSource)
        {
            if (item.IsChecked)
            {
                str = str + item.DepartmentName + ",";
            }
        }
        if (str != "")
            str = str.Substring(0, str.Length - 1);
        tbDepartment.Text = str;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Savalia Manoj M
Software Developer (Senior)
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to make this as a control?memberzhonghuafy24 Dec '12 - 18:46 
AnswerRe: How to make this as a control?memberSavalia Manoj M25 Dec '12 - 17:04 
QuestionIntegration in Lightswitch application?memberMember 953543222 Oct '12 - 2:57 
AnswerRe: Integration in Lightswitch application?memberSavalia Manoj M22 Oct '12 - 18:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 28 Nov 2012
Article Copyright 2012 by Savalia Manoj M
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid