#980 – Binding ListBox Selection to Property on Bound Object

Normally, when you use a ListBox, the ListBox itself persists the record of which items are selected.  You might also want to persist information about which items are selected within the actual data objects that you’re binding to.

You can bind the IsSelected property of each list box item to a boolean property on the data object as shown below.

        <ListBox Name="lbActors" Margin="15" Width="200" Height="190"
                 SelectionMode="Multiple"
                 ItemsSource="{Binding ActorList}">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="IsSelected" Value="{Binding IsFav}"/>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>

Now, as you select/unselect items, the IsFav property in the corresponding Actor object is set or cleared.  Also, when your application starts, the ListBox selection will initially be set to reflect the value of this property.

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sbInfo = new StringBuilder();

            // Who are favorites?
            foreach (Actor a in lbActors.Items)
            {
                if (a.IsFav)
                    sbInfo.AppendLine(a.FullName);
            }

            MessageBox.Show(sbInfo.ToString());
        }

980-001

Advertisements

#979 – Selecting Items in a ListBox Programmatically

The ListBoxItem object, representing a single item in a ListBox, contains an IsSelected property that you can read in order to determine whether the item is selected.  The IsSelected property can be read from to determine if the item is selected.  It can also be written to, to select the item.

The code below selects every other item in a ListBox.  Note that we need to call the ItemContainerGenerator.ContainerFromIndex helper method in order to get the ListBoxItem for each item.

private void btnWrite_Click(object sender, RoutedEventArgs e)
{
    // Select every other item, starting with
    // the first.
    int i = 0;
    while (i < lbActors.Items.Count)
    {
        // Get item's ListBoxItem
        ListBoxItem lbi = (ListBoxItem)lbActors.ItemContainerGenerator.ContainerFromIndex(i);
        lbi.IsSelected = true;
        i += 2;
    }
}

979-001

#978 – Discovering Whether an Item in a ListBox Is Selected

The ListBoxItem object, representing a single item in a ListBox, contains an IsSelected property that you can read in order to determine whether the item is selected.

If you use data binding to populate the list, the objects in the ListBox’s Items property will not be ListBoxItem instances, but will have a type matching the bound object.

You can use the ContainerFromItem helper method to get the corresponding ListBoxItem so that you can then query it to determine if the object is selected.

        private void btnRead_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sbInfo = new StringBuilder();

            // Each item is an Actor
            foreach (Actor a in lbActors.Items)
            {
                // Get item's ListBoxItem
                ListBoxItem lbi =
                  (ListBoxItem)lbActors.ItemContainerGenerator.ContainerFromItem(a);

                if (lbi.IsSelected)
                    sbInfo.Append("YES ");
                else
                    sbInfo.Append("no ");
            }

            MessageBox.Show(sbInfo.ToString());
        }

978-001