0

ObservableCollectionからデータをプルする単純なListViewがあり、CollectionViewSourceを使用してGroupPropertyDescriptionを設定し、行がグループ化されて表示されるようにしています。次のスタイルを使用して、各グループにエキスパンダーを使用しました。

<Style TargetType="{x:Type GroupItem}" x:Key="listViewGroupStyle">
    <Setter Property="Margin" Value="0 0 0 5" />
    <Setter Property="Template" >
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                <Expander IsExpanded="False" BorderBrush="{Binding Name, Converter={StaticResource statusForegroundConverter}}" BorderThickness="2" Margin="1">
                    <Expander.Header>
                        <Border BorderThickness="3" BorderBrush="{Binding Name, Converter={StaticResource statusBackgroundConverter}}" CornerRadius="6">
                            <DockPanel TextBlock.FontWeight="Bold" TextBlock.Foreground="{Binding Name, Converter={StaticResource statusForegroundConverter}}">
                                <TextBlock Text="{Binding Name, Converter={StaticResource statusStringConverter}}" Margin="5 2 2 2" />
                                <TextBlock Text=" - " Margin="2" />
                                <TextBlock Text="{Binding Path=ItemCount}" Margin="0 2 5 2"/>
                            </DockPanel>
                        </Border>
                    </Expander.Header>
                    <Expander.Content>
                        <ItemsPresenter />
                    </Expander.Content>
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

私が抱えている問題は、実行時にコレクションが頻繁に更新され、そのたびに、開いているかどうかに関係なく、すべてのグループが折りたたまれてしまうことです。

コレクションが再割り当てされた後、ListViewの外観を維持する方法はありますか?

4

1 に答える 1

0

私は解決策を見つけました。詳細のほとんどはここにあります。

http://social.msdn.microsoft.com/Forums/en/wpf/thread/f022425b-f685-4a7a-91df-828103871ebc

重要な点は、ItemsSourceが再割り当てされると、関連付けられたコントロールが無効になり、再描画されることです。ただし、ObservableCollectionを使用していたため、新しいデータを一時リストに蓄積し、必要に応じて元のリストにオブジェクトを追加または削除する方がはるかに簡単です。

ObservableCollection<xxx> tempBuilds = new ObservableCollection<xxx>();

IEnumerable<xxx> addRange = tempBuilds.Except(_filteredBuilds);
IEnumerable<xxx> removeRange = _filteredBuilds.Except(tempBuilds);

addRange.ToList().ForEach(bm => _filteredBuilds.Add(bm));
removeRange.ToList().ForEach(bm => _filteredBuilds.Remove(bm));
于 2010-08-16T10:47:32.190 に答える