This project has moved. For the latest updates, please go here.

CheckListBox, Selecting (checking) items by code (c#, manually)

Apr 12, 2013 at 8:24 PM
Hello!
I have had a look around but to my disappointment I cannot find a way to select (check) any one chosen item in my collection like you can in windows forms applications.
Now, is there a way to check/uncheck all items and or check a specific item?
All help is greatly appreciated!
May 1, 2013 at 9:34 AM
OhRlyMrTsla wrote:
Hello!
I have had a look around but to my disappointment I cannot find a way to select (check) any one chosen item in my collection like you can in windows forms applications.
Now, is there a way to check/uncheck all items and or check a specific item?
All help is greatly appreciated!
Bind the SelectedMemberPath of the CheckListBox to a boolean property of the object you're showing and set / unset that property in code.
Jul 9, 2013 at 5:31 PM
Edited Jul 9, 2013 at 5:33 PM
Help! I am a very new newbie to WPF and I am having the same problem. When I initially load my CheckListBox (with a list of directories), I want all items to be checked but no matter what I do, it shows the items but only the last one is checked. How do I make all of them checked? Do I have to change some kind of selection mode from single to multi somehow? What am I doing wrong?

Thanks!

Here's the XAML:
        <wpfx:CheckListBox x:Name="searchDirectoriesListBox" Margin="12,12,0,0" 
                           SelectedMemberPath="IsChecked">
        </wpfx:CheckListBox>
In my code-behind, I load the CheckListBox with a bunch of directories (stored in List<string> searchDirs) like this:
int id = 0;
foreach (string dir in searchDirs)
{
// Trying to make them all checked as I add them
CheckBoxItem cbi = new CheckBoxItem(id, dir, true);
searchDirectoriesListBox.Items.Add(cbi);
searchDirectoriesListBox.SelectedItem = cbi; // This makes it so only the last item in the list is selected
id++;
}

Here is what a CheckListBox looks like:
public class CheckBoxItem
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool IsChecked { get; set; }

    public CheckBoxItem(int id, string name, bool isChecked)
    {
        ID = id;
        Name = name;
        IsChecked = isChecked;
    }

    public override string ToString()
    {
        return Name;
    }
}
Developer
Jul 10, 2013 at 12:38 PM
Try using "searchDirectoriesListBox.SelectedItems.Add( cbi )" to add an item in the SelectedItems collection of the CheckListBox. To make sure the SelectedItems is set, try do do it in a Loaded callback.
public MyView()
    {
      InitializeComponent();
      
      this.Loaded += new System.Windows.RoutedEventHandler( MyView_Loaded );
    }

void MyView_Loaded( object sender, System.Windows.RoutedEventArgs e )
    {
        foreach (string dir in searchDirs)
       {
        // Trying to make them all checked as I add them
        CheckBoxItem cbi = new CheckBoxItem(id, dir, true);
        searchDirectoriesListBox.SelectedItems.Add(cbi);
        id++;
        }
    }
Jul 10, 2013 at 12:42 PM
That worked great, thanks so much!