ListView分组未在详细视图中更新

时间:2019-03-06 08:24:23

标签: c# wpf listview grouping master-detail

我有一个ObservableCollection,它绑定到Master-Detail ListView。 Detail-View应按ObservableCollection的特定成员分组。到目前为止,一切都按预期工作,但是当从“主视图”中选择另一个项目并且“详细视图”正在更新时,该项目不再分组。我在做什么错了?

XAML主视图:

        <StackPanel>
            <ListView x:Name="ListViewMaster" Height="100" Width="120" ItemsSource="{Binding ListResult}" IsSynchronizedWithCurrentItem="True" SelectionChanged="ListViewMaster_SelectionChanged">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="ID" Width="70">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding ID}"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        </StackPanel>

XAML详细信息视图:

<ListView Margin="10" x:Name="ListViewDetail" ItemsSource="{Binding ElementName=ListViewMaster, Path=SelectedItem.ListResultDetail}"  >

                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="name" Width="150">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding name}"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="Var" Width="150">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <local:MyFormattedTextControl Text="{Binding var}" PartFlags="{Binding colorCode}"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>

                <ListView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name, Converter={StaticResource EnumarableToDescriptionConverter}}" FontWeight="Bold" FontSize="14"/>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                    </GroupStyle>
                </ListView.GroupStyle>

            </ListView>

构造函数后的代码:

    public MyComparator() {
        _myViewModel = BPTViewModel.Instance;
        this.DataContext = _myViewModel;
        InitializeComponent();

        CollectionView view1 = (CollectionView)CollectionViewSource.GetDefaultView(ListViewDetail.ItemsSource);
        PropertyGroupDescription groupDescription = new PropertyGroupDescription("Type");
        view1.GroupDescriptions.Add(groupDescription);
    }

0 个答案:

没有答案