【发布时间】:2011-02-18 00:32:51
【问题描述】:
我有一个相当棘手的问题:
我正在使用 ListView 控件,其中 ItemsSource 设置为 CollectionViewSource,包括 PropertyGroupDescription 来对 ListView 元素进行分组。 CollectionViewSource 如下所示:
<CollectionViewSource x:Key="ListViewObjects">
<CollectionViewSource.Source>
<Binding Path="CurrentListViewData"/>
</CollectionViewSource.Source>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="ObjectType" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
在 ListView 中,我使用自定义组标题,如下所示:
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True">
<Expander.Header>
<DockPanel>
<TextBlock Text="{Binding Path=Items[0].ObjectType />
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
您可以看到 Expander 的 IsExpanded 属性设置为 true。这意味着每当刷新 ListView 时,所有 Expander 控件都会展开。
不过,我确实想保存每个 Expander 的最后一个状态。我还没有找到一种方法来保存每个 ObjectType 的 Expander 状态列表。我正在尝试绑定 HashTable 和 Converter,但未能将 ObjectType 作为 ConverterParameter 提供,因为它始终作为字符串传递。但无论如何,这可能不是解决方案。
有人可以给我一个提示或解决方案的想法吗? :)
【问题讨论】:
-
你确定你真正的问题不是你当前的设计强迫你刷新列表视图吗?
-
ListView 通过调用 NotifyPropertyChanged("CurrentListViewData") 进行刷新。我不知道该怎么做。
标签: c# wpf data-binding xaml