具有CollectionViewSource的WPF ListView未在ViewModel中获取SelectedItem

时间:2017-06-24 14:24:49

标签: c# wpf listview mvvm

我有一个ListView我正在使用CollectionViewSource进行分组,但我似乎无法将所选项目返回到ViewModel中。我需要做什么才能获得用户选择的项目? SelectedItem =“{Binding SelectedComparatorGroupItem}”在使用collectionviewSource时似乎不起作用,我尝试了IsSynchronizedWithCurrentItem =“True”,但这没有帮助。

                <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>

1 个答案:

答案 0 :(得分:3)

您将DataContext的{​​{1}}设置为您的本地ListView。这意味着将CollectionViewSource绑定到SelectedItem将在SelectedComparatorGroupItem而不是您的视图模型中查找SelectedComparatorGroupItem属性。您应该在输出窗口中看到某种绑定错误。

不要设置CollectionViewSource,让它自然流过。您需要做的就是将DataContext绑定到ItemsSource

认为会这样做,但我没有打开VS或访问您的ViewModel来验证:

CollectionViewSource

作为一般规则,如果您要设置某些内容的<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"> ,那么您就不会按照WPF要求的方式进行操作。通常情况下,我最终设置它的唯一地方就是当我懒惰时使用DataContext