Reputation: 1764
I Have a ListView I am grouping using a CollectionViewSource, but I cant seem to get the selected item back into the ViewModel. What do I need to do to get the item the user selects? SelectedItem="{Binding SelectedComparatorGroupItem}" does not appear to work when using a collectionviewSource, I tried IsSynchronizedWithCurrentItem="True" but that did not help.
<Grid>
<Grid.Resources>
<CollectionViewSource x:Key="NumberGroups"
Source="{Binding Path=ComparatorGroupItemList}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Group" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Grid.Resources>
<ListView x:Name="lvNumbers"
DataContext="{StaticResource NumberGroups}"
ItemsSource="{Binding IsAsync=True}"
IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SelectedComparatorGroupItem}"
VirtualizingPanel.IsContainerVirtualizable="True"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.IsVirtualizingWhenGrouping="True">
<ListView.View>
<GridView>
<GridViewColumn Header="Number" DisplayMemberBinding="{Binding Number}"/>
<GridViewColumn Header="# Found" DisplayMemberBinding="{Binding NumberFound}"/>
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="False">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"
Margin="3"
FontSize="18" FontWeight="Bold"
/>
<TextBlock Text="{Binding ItemCount}"
Margin="3"
FontSize="18" FontWeight="Bold"
/>
</StackPanel>
</Expander.Header>
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
</Grid>
Upvotes: 1
Views: 2818
Reputation: 16991
You set the DataContext
of the ListView
to your local CollectionViewSource
. That means that binding SelectedItem
to SelectedComparatorGroupItem
is going to look for the SelectedComparatorGroupItem
property on the CollectionViewSource
, not your viewmodel. You should be seeing some kind of binding error in the output window from that.
Don't set the DataContext
, just let it flow through naturally. All you should need to do is bind the ItemsSource
to the CollectionViewSource
.
I think this will do it, but I don't have VS open, or access to your ViewModel, to verify:
<ListView x:Name="lvNumbers"
ItemsSource="{Binding Source={StaticResource NumberGroups}, IsAsync=True}"
IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SelectedComparatorGroupItem}"
VirtualizingPanel.IsContainerVirtualizable="True"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.IsVirtualizingWhenGrouping="True">
As a general rule, if you are ever setting the DataContext
of something, you are not doing the way WPF wants you to do it. Usually the only place I end up setting it is when I am being lazy with a UserControl
.
Upvotes: 4