gpt4 book ai didi

c# - 如何在 ListView 的组标题中保存 IsExpanded 状态

转载 作者:可可西里 更新时间:2023-11-01 03:07:10 25 4
gpt4 key购买 nike

我有一个相当棘手的问题:

我正在使用 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 的扩展器状态列表。我正在尝试使用绑定(bind)的 HashTable 和 Converter,但我未能将 ObjectType 作为 ConverterParameter 提供,因为它始终作为字符串传递。但这可能不是解决方案。

有人可以给我一个解决方案的提示或想法吗? :)

最佳答案

如评论中所述,接受的答案是错误的。我编写了以下实现所需功能的行为:

public class PersistGroupExpandedStateBehavior : Behavior<Expander>
{
#region Static Fields

public static readonly DependencyProperty GroupNameProperty = DependencyProperty.Register(
"GroupName",
typeof(object),
typeof(PersistGroupExpandedStateBehavior),
new PropertyMetadata(default(object)));

private static readonly DependencyProperty ExpandedStateStoreProperty =
DependencyProperty.RegisterAttached(
"ExpandedStateStore",
typeof(IDictionary<object, bool>),
typeof(PersistGroupExpandedStateBehavior),
new PropertyMetadata(default(IDictionary<object, bool>)));

#endregion

#region Public Properties

public object GroupName
{
get
{
return (object)this.GetValue(GroupNameProperty);
}

set
{
this.SetValue(GroupNameProperty, value);
}
}

#endregion

#region Methods

protected override void OnAttached()
{
base.OnAttached();

bool? expanded = this.GetExpandedState();

if (expanded != null)
{
this.AssociatedObject.IsExpanded = expanded.Value;
}

this.AssociatedObject.Expanded += this.OnExpanded;
this.AssociatedObject.Collapsed += this.OnCollapsed;
}

protected override void OnDetaching()
{
this.AssociatedObject.Expanded -= this.OnExpanded;
this.AssociatedObject.Collapsed -= this.OnCollapsed;

base.OnDetaching();
}

private ItemsControl FindItemsControl()
{
DependencyObject current = this.AssociatedObject;

while (current != null && !(current is ItemsControl))
{
current = VisualTreeHelper.GetParent(current);
}

if (current == null)
{
return null;
}

return current as ItemsControl;
}

private bool? GetExpandedState()
{
var dict = this.GetExpandedStateStore();

if (!dict.ContainsKey(this.GroupName))
{
return null;
}

return dict[this.GroupName];
}

private IDictionary<object, bool> GetExpandedStateStore()
{
ItemsControl itemsControl = this.FindItemsControl();

if (itemsControl == null)
{
throw new Exception(
"Behavior needs to be attached to an Expander that is contained inside an ItemsControl");
}

var dict = (IDictionary<object, bool>)itemsControl.GetValue(ExpandedStateStoreProperty);

if (dict == null)
{
dict = new Dictionary<object, bool>();
itemsControl.SetValue(ExpandedStateStoreProperty, dict);
}

return dict;
}

private void OnCollapsed(object sender, RoutedEventArgs e)
{
this.SetExpanded(false);
}

private void OnExpanded(object sender, RoutedEventArgs e)
{
this.SetExpanded(true);
}

private void SetExpanded(bool expanded)
{
var dict = this.GetExpandedStateStore();

dict[this.GroupName] = expanded;
}

#endregion
}

它将字典附加到包含的 ItemsControl,它保存每个组项目的展开状态。这将是持久的,即使控件中的项目发生变化也是如此。

用法:

<Expander>
<i:Interaction.Behaviors>
<behaviors:PersistGroupExpandedStateBehavior GroupName="{Binding Name}" />
</i:Interaction.Behaviors>
...
</Expander>

关于c# - 如何在 ListView 的组标题中保存 IsExpanded 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2808777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com