WPF listbox checkbox

In the simplest form it should look like this: XAML: In your code, for example:lst.ItemsSource = new List[] { new KeyValuePair[1, "1"], new KeyValuePair[2, "2"], }; myObj.IsChecked]] MessageBox.Show[tObj.Name];

In this article we'll talk about how to render check boxes inside a ListView. You can use this feature for example, for presenting a list of options to ends users and allowing them to select one option or multiple options. The way you do this is by using DataTemplates inside the ListView and then do some basic data binding to show the controls. In the sample below I've also added functionality to hide the headers for the ListView. The final product we'll essentially look like this:

Adding check boxes to the ListView

To add the check box to each list view item, we need to change the XAML generated for the list and include and add a GridView and will contain two columns: one for the check box and one for the label. Below you can see the XAML code for this, specifically inside the tag:

Hiding the column headers For hiding the headers we just need to style the GridView header and set the Visiblity property to Collapsed as shown in the XAML below:

Binding the check boxes to the ListView

For the binding, I created a class that has two properties: Text and IsChecked. These properties hold the values to the two columns our ListView has. Text will have the actual label to show in the second column and IsChecked determines if the checkbox is checked. It actually represents the object for each row in the list view. It's also implementing the INotifyPropertyChanged that will allow the ListView to update itself when a value in the object changes. You can see the code below:

public class CheckBoxListViewItem : INotifyPropertyChanged { private bool isChecked; private string text; public bool IsChecked { get { return isChecked; } set { if [isChecked == value] return; isChecked = value; RaisePropertyChanged["IsChecked"]; } } public String Text { get { return text; } set { if [text == value] return; text = value; RaisePropertyChanged["Text"]; } } public CheckBoxListViewItem[string t, bool c] { this.Text = t; this.IsChecked = c; } public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged[string propName] { PropertyChangedEventHandler eh = PropertyChanged; if [eh != null] { eh[this, new PropertyChangedEventArgs[propName]]; } } }

Now we'll just create some test objects when the Window is created for testing:

ObservableCollection items = new ObservableCollection[]; items.Add[new CheckBoxListViewItem["Red",false]]; items.Add[new CheckBoxListViewItem["Green", true]]; items.Add[new CheckBoxListViewItem["Blue", false]]; myListView.ItemsSource = items;

This gives you the result below:

Check all button for the ListView

Adding a Check All button is also pretty simple. On click event for the button we just need to loop through the check boxes change the IsChecked property to true. This will automatically notify the ListView and update itself. Below you can see the code:

private void CheckAllButton_Click[object sender, RoutedEventArgs e] { foreach [CheckBoxListViewItem o in myListView.ItemsSource] { o.IsChecked = true; } }

Add Comment

Hello JeffSM,

The RadListBox control does not provide a CheckBoxes property out of the box, however, as my colleague demonstrated, such a setup is not hard to accomplish.

Could you please clarify whether using an approach such as the one Kalin provided would work for you?

If that is not the case, feel free to open a new feature request in our feedback portal regarding such functionality and if the demand for such a feature is high, we will consider implementing it in future releases.

Regards, Dilyan Traykov

Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.

If your client is using the same source code, this can just be an embedded view in the application. If you need to port this as a DLL to your client, which can be used in other WPF applications [by adding reference], please find the link below which will guide you through the process.

//www.codeproject.com/Articles/32825/How-to-Creating-a-WPF-User-Control-using-it-in-a-W

If a post answers your question, please click "Mark As Answer" and "Mark as Helpful" on that post.

In order to use CheckBox ListBox in a WPF application, we should use custom ListBox list.

There are three steps to accomplish this:

1. Create a class which will represent items of the ListBox.

2. Create a List object consisting of the custom item class defined in step 1.

3. Add ListBox.ItemTemplate into element to create CheckBox ListBox.

4. Create a custom MultiValueConverter if you want to initialize the CheckBox items to be selected or not.

Step 1: Create Country class for ListBox items

internal class Country { public string Name { get; set; } public bool Active { get; set; } }

Step 2: Initiliaze ListBox

private static void initialize[] { List countryList = new List[]; Country country = new Country[]; countryTr.Name ="Turkey"; countryTr.Active = true; Country contryUS=new Country[]; contryUS.Name = "America"; contryUS.Active = false; countryList.add[countryTr]; countryList.add[countryUS]; //countryTr is active, so add this object to SelectedItems collection lstCountries.SelectedItems.Add[countryTr]; lstCountries.ItemsSource = countryList; }

Step 3: Custom ListBox element in XAML file

Line 4: We specified SelectionMode as Multiple, so we can choose multiple checkboxes.

Line 5, 6, 9: These lines are used to make ListBox style as WrapPanel.

Line 18: It is necessary to get selected checkboxes. To get SelectedItems: var selectedCheckBoxList = lstCountries.SelectedItems;

Line 19: Path="Active" is necessary when initialize CheckBox items.

Step 4: CollectionToBoolConverter Class

public class CollectionToBoolConverter : IMultiValueConverter { /// /// Runs when initialization /// /// Binding values /// Merged value /// /// /// public object Convert[object[] values, Type targetType, object parameter, CultureInfo culture] { bool listBoxValue = [bool] values[0]; bool initializedValue = [bool] values[1]; return listBoxValue || initializedValue; } /// /// Runs when any ListBox element thats CheckBox element is clicked. /// This method only runs when Mode is "TwoWay" /// /// /// /// /// /// public object[] ConvertBack[object value, Type[] targetTypes, object parameter, CultureInfo culture] { /*bool isChecked = [bool] value; object[] resultObjects=new object[2]; resultObjects[0] = isChecked; resultObjects[1] = isChecked; return resultObjects; */ return null; } }

Video liên quan

Chủ Đề