Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView + MultipleSelect + MVVM =?

If I were to say "the heck with it!", I could just give my ListView with SelectionMode="Multiple" a name, and be able to get all of the selected items very easily. But I'm trying to stick to MVVM as much as possible, and I want to somehow databind to an ObservableCollection that holds the value from the Name column for each selected item. How in the world do you do this? Single selection is simple, but the multi selection solution is not obvious to me with my current WPF / MVVM knowledge.

I read this question on SO, and while it does give me some good insight, I don't know how to add the necessary binding to a row, because I am using a ListView with a GridView as its View, not a ListBox.

Here's what my XAML basically looks like:

<ListView DockPanel.Dock="Top" ItemsSource="{Binding ClientPreview}" SelectionMode="Multiple">
    <ListView.View>
        <GridView AllowsColumnReorder="False">
            <GridViewColumn Header="Name">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Name}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Address">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Address}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

It sounds like the right thing to do is to databind each row's IsSelected property to each object stored in the ObservableCollection I'm databinding to. I just haven't figured out how to do this.

like image 382
Dave Avatar asked Mar 24 '10 00:03

Dave


1 Answers

Write ItemContainerstyle on the ListView and put a Setter to do the binding to your ViewModel 'IsSelected' property

<Style TargetType="{x:Type ListViewItem}">
   <Setter Property="IsSelected" Value="{Binding IsSelected,Mode=OneWayToSource}"/>
like image 116
Jobi Joy Avatar answered Oct 11 '22 20:10

Jobi Joy