Quantcast
Channel: Extended WPF Toolkit™ Community Edition
Viewing all articles
Browse latest Browse all 4964

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

$
0
0
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;
    }
}

Viewing all articles
Browse latest Browse all 4964

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>