WPF - 在 ListView 中维护组样式

发布于 2024-09-12 14:13:40 字数 1785 浏览 3 评论 0原文

我有一个简单的 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 的外观?

I've got a simple ListView which pulls data from an ObservableCollection and I'm using a CollectionViewSource to a set a GroupPropertyDescription so that my rows appear grouped. I've used the following style to use an expander for each group:

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

The problem I have is that my collection is updated frequently during runtime and everytime it is, all the groups get collapsed regardless of whether or not I had some open.

Is there a way for me to maintain the appearance of the ListView after my collection has been reassigned?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅冕 2024-09-19 14:13:40

我找到了一个解决方案,其中大部分详细信息如下:

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

I've found a solution, with most of the details here:

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

The main point is that when an ItemsSource is reassigned, it invalidates the associated control, causing it to redraw.
However, as I was using an ObservableCollection, it's far easier to accumulate your new data in a temporary list and add or remove objects from the original list as necessary:

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文