Programmatically selecting Items/Indexes in a ListBox
Asked Answered
N

6

17

In WPF, I'd like to set the selected indexes of a System.Windows.Controls.ListBox

I best way I've found so far is to remove all the items from the control, insert the selected, call SelectAll(), then insert the rest, but this solution neither works in my situation nor is very efficient.

So, how do you set items in a Listbox to be selected, programmatically?

Naturally answered 6/5, 2009 at 19:27 Comment(0)
D
12

One way you can do this is to add a Selected field to your data object. Then you need to overide the default listboxitem style and bind the isselected property to the Selected property in your object. Then you just need to go through your data items and update the Selected value.

If you don't implement that Selected property as a dependency property, you need your class to implented the INotifyPropertyChanged interface and raise the propertychanged event when you set the value.

Dulla answered 6/5, 2009 at 19:34 Comment(2)
Thanks !...This worked fine. I'll just add a Code segment that I used below.Farleigh
actually this is overcomplicated when compared to danlash's below.Genaro
M
33

You can set multiple items as selected by using the SelectedItems collection. This isn't by index, but by what you have bound:

foreach (var boundObject in objectsBoundToListBox)
{
    ListBox.SelectedItems.Add(boundObject);
}
Macropterous answered 11/6, 2009 at 16:14 Comment(1)
This will raise the SelectionChanged event once for each item added. When you select a group of items manually with shift it only gets raised once for the whole group.Pia
D
12

One way you can do this is to add a Selected field to your data object. Then you need to overide the default listboxitem style and bind the isselected property to the Selected property in your object. Then you just need to go through your data items and update the Selected value.

If you don't implement that Selected property as a dependency property, you need your class to implented the INotifyPropertyChanged interface and raise the propertychanged event when you set the value.

Dulla answered 6/5, 2009 at 19:34 Comment(2)
Thanks !...This worked fine. I'll just add a Code segment that I used below.Farleigh
actually this is overcomplicated when compared to danlash's below.Genaro
S
5

You have to do this:

ListBoxObject.SelectedItem = ListBoxObject.Items.GetItemAt(itemIndex);

Where itemIndex would be the item you want to select. If you want to select multiple items, you need to use the ListBox.SelectedIndexCollection property.

Sawtelle answered 6/5, 2009 at 19:44 Comment(2)
System.Windows.Controls.ListBox is not like System.Windows.Forms.ListBox in contining a SelectedIndexCollection propertyNaturally
but not for mor than one item - and SelectedItems is read onlySibylle
F
3

You can do this for multiple sections:

ListBoxObject.SelectedItems.Add(ListBoxObject.Items.GetItemAt(i));

Where i is the item index.

Fortdefrance answered 25/7, 2014 at 20:57 Comment(0)
F
1

Thanks to mdm20. My case was actually checking a CheckBox within the ListBox, and this Dependency Property worked like a charm. I had to inherit my custom class from DependencyObject and implement the property

public class ProjectListItem : DependencyObject{ 

    public Boolean IsChecked
    {
        get { return (Boolean)this.GetValue(CheckedProperty); }
        set { this.SetValue(CheckedProperty, value); }
    }
    public static readonly DependencyProperty CheckedProperty =
        DependencyProperty.Register("IsChecked", typeof(Boolean), typeof(ProjectListItem), 
                                    new PropertyMetadata(false));
}
Farleigh answered 21/11, 2010 at 3:25 Comment(0)
S
-4

how to programmatically select multiple items in listbox in wpf

foreach (var boundObject in objectsBoundToListBox)
{
    ListBox.SelectedItems.Add(boundObject);
}
Schutzstaffel answered 10/9, 2009 at 14:43 Comment(1)
Did you actually cut and paste this from danlash on this thread.Hooded

© 2022 - 2024 — McMap. All rights reserved.