David Veeneman
David Veeneman

Reputation: 19132

Databinding a ListBox with SelectionMode = Multiple

I have a WPF ListBox that I would like to

These two requirements appear to be incompatible. My view model has an ObservableCollection<T> property to bind to this ListBox; I set up a binding in XAML from the property to the ListBox.SelectedItems property. When I compiled, I got an error saying that the SelectedItems property was read only and could not be set from XAML.

Am I binding to the wrong control property? Is there a way to bind a multiple-selection ListBox in XAML to a view model collection property? Thanks for your help.

Upvotes: 15

Views: 15435

Answers (3)

Brian Hinchey
Brian Hinchey

Reputation: 3691

I have posted a technique for allowing a read-only binding to the SelectedItems property of a WPF DataGrid just by extending the DataGrid that I believe could easily be co-opted for a ListBox. You can see my post at https://stackoverflow.com/a/16953833/62278

Upvotes: 0

David Veeneman
David Veeneman

Reputation: 19132

I ended up using a bit of code-behind in a SelectionChanged event handler to set the view model property. Simpler than creating object wrappers.

Upvotes: 7

Aran Mulholland
Aran Mulholland

Reputation: 23945

What you can do to get around this:

Create an IsSelected property on the items you are displaying in the list. Im assuming these are represented by a view model as well. So it shouldn't be a drama to add an extra property. If they are just business objects consider using a wrapper class to painlessly wrap them in view model items (shameless plug - here is one such system. I use it daily to wrap my business objects)

Use an items container style to bind the IsSelected property of the list box item to the IsSelected property of the items you are displaying, like so

    <ListBox.ItemContainerStyle>
       <!-- This Style binds a ListBoxItem to a the ViewModelItem. -->
       <Style
          TargetType="{x:Type ListBoxItem}">
          <Setter
             Property="IsSelected"
             Value="{Binding IsSelected, Mode=TwoWay}" />
       </Style>
    </ListBox.ItemContainerStyle>

(might need a based on attribute on the style, not sure)

Now whenever a list box item gets selected your view model will know about it and can update its internal collection as required.

Upvotes: 23

Related Questions