#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"
                 ItemsSource="{Binding ActorList}">
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="IsSelected" Value="{Binding IsFav}"/>

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)




About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #980 – Binding ListBox Selection to Property on Bound Object

  1. sshumakov says:

    Actually this implementation isn’t suitable for lists which have big amount of items (e.g. 2-5 k). I tried to bind to IsSelected property of each item in ListBox/DataGrid and there was big performance hit when you press CTRL + A.

    The solution was to write custom behavior which hooks to ListBox/DataGrid selection related events and propagates selection changes where it’s needed (view model in my case).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: