Click here to Skip to main content
16,023,224 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
I am working on WPF application.Here i am using
CheckListBox
for some functionalty.
Here i am binding data into
CheckListBox
.But i am not doing any checkbox related functionalities.
Like defaultly checked all the items or get checked items values.

What I have tried:

I tried so many articles but it's not worked for me.
Posted
Updated 19-Aug-19 2:26am

1 solution

It's not clear what is your goal or what you want to do with checkbox list but I'll give you a very simple example how it can be done
Try this Create wpf app and replace MainWindow code like below

<Window x:Class="WpfApp2.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
		DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="450" Width="800">
	<Grid>
		<ListBox ItemsSource="{Binding CheckList}" Grid.Column="1" Grid.Row="6" Height="200">
			<ListBox.ItemTemplate>
				<DataTemplate>
					<CheckBox Name="chkitems" Content="{Binding TheText}" IsChecked="{Binding IsSelected }" />
				</DataTemplate>
			</ListBox.ItemTemplate>
		</ListBox>
	</Grid>
</Window>


C#
public partial class MainWindow : Window, INotifyPropertyChanged
{
	List<CheckListItem> checkList;
		
	public event PropertyChangedEventHandler PropertyChanged;
	protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
	{
		PropertyChanged?.Invoke(this, e);
	}
		
	public MainWindow()
	{
		InitializeComponent();
		CheckList = GenerateTestData();
	}

	public List<CheckListItem> CheckList
	{
		get { return checkList; }
		set { 
                       checkList = value; 
                       OnPropertyChanged(new PropertyChangedEventArgs("CheckList")); 
            }
	}

	List<CheckListItem> GenerateTestData()
	{
		var checkListItems = new List<CheckListItem>() {
			new CheckListItem { TheText = "Text 1", IsSelected = false },
			new CheckListItem { TheText = "Text 2", IsSelected = true },
			new CheckListItem { TheText = "Text 3", IsSelected = false }
		};

		return checkListItems;
	}
}

public class CheckListItem
{
	public string TheText { get; set; }
	public bool IsSelected { get; set; }
}
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900