【问题标题】:WPF - Maintain Group Style in ListViewWPF - 在 ListView 中维护组样式
【发布时间】:2010-08-03 11:02:00
【问题描述】:

我有一个简单的 ListView,它从 ObservableCollection 中提取数据,我使用 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 的外观?

【问题讨论】:

    标签: wpf listview expander groupstyle


    【解决方案1】:

    我找到了一个解决方案,大部分细节都在这里:

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

    【讨论】:

      猜你喜欢
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多