Skip to content

Commit 8347076

Browse files
committed
Persist expanded state of GroupingBrid groups after changes to the grouping.
1 parent bcb0d22 commit 8347076

File tree

3 files changed

+121
-35
lines changed

3 files changed

+121
-35
lines changed

Rubberduck.Core/UI/Controls/GroupingGrid.xaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6-
xmlns:local="clr-namespace:Rubberduck.UI.Controls" x:Class="Rubberduck.UI.Controls.GroupingGrid"
6+
xmlns:local="clr-namespace:Rubberduck.UI.Controls"
7+
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
8+
x:Class="Rubberduck.UI.Controls.GroupingGrid"
79
mc:Ignorable="d"
810
d:DesignHeight="300" d:DesignWidth="300">
911
<DataGrid.Resources>
@@ -76,6 +78,9 @@
7678
</StackPanel>
7779
</Expander.Header>
7880
<ItemsPresenter Margin="25,0,0,0" />
81+
<i:Interaction.Behaviors>
82+
<local:PersistGroupExpandedStateBehavior GroupName="{Binding Name}" />
83+
</i:Interaction.Behaviors>
7984
</Expander>
8085
</ControlTemplate>
8186
</Setter.Value>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System.Collections.Generic;
2+
using System.Windows;
3+
using System.Windows.Controls;
4+
using System.Windows.Interactivity;
5+
using GongSolutions.Wpf.DragDrop.Utilities;
6+
7+
namespace Rubberduck.UI.Controls
8+
{
9+
// Courtesy of @aKzenT - this is a heavily refactored implementation of https://stackoverflow.com/a/15924044/4088852
10+
public class PersistGroupExpandedStateBehavior : Behavior<Expander>
11+
{
12+
public static readonly DependencyProperty GroupNameProperty = DependencyProperty.Register(
13+
"GroupName",
14+
typeof(object),
15+
typeof(PersistGroupExpandedStateBehavior),
16+
new PropertyMetadata(default(object)));
17+
18+
private static readonly DependencyProperty ExpandedStateStoreProperty =
19+
DependencyProperty.RegisterAttached(
20+
"ExpandedStateStore",
21+
typeof(IDictionary<object, bool>),
22+
typeof(PersistGroupExpandedStateBehavior),
23+
new PropertyMetadata(default(IDictionary<object, bool>)));
24+
25+
public object GroupName
26+
{
27+
get => GetValue(GroupNameProperty);
28+
set => SetValue(GroupNameProperty, value);
29+
}
30+
31+
protected override void OnAttached()
32+
{
33+
base.OnAttached();
34+
35+
var states = GetExpandedStateStore();
36+
var expanded = !states.ContainsKey(GroupName ?? string.Empty) ? (bool?)null : states[GroupName ?? string.Empty];
37+
38+
if (expanded != null)
39+
{
40+
AssociatedObject.IsExpanded = expanded.Value;
41+
}
42+
43+
AssociatedObject.Expanded += OnExpanded;
44+
AssociatedObject.Collapsed += OnCollapsed;
45+
}
46+
47+
protected override void OnDetaching()
48+
{
49+
AssociatedObject.Expanded -= OnExpanded;
50+
AssociatedObject.Collapsed -= OnCollapsed;
51+
52+
base.OnDetaching();
53+
}
54+
55+
private void OnCollapsed(object sender, RoutedEventArgs e) => SetExpanded(false);
56+
57+
private void OnExpanded(object sender, RoutedEventArgs e) => SetExpanded(true);
58+
59+
private void SetExpanded(bool expanded) => GetExpandedStateStore()[GroupName ?? string.Empty] = expanded;
60+
61+
private IDictionary<object, bool> GetExpandedStateStore()
62+
{
63+
if (!(AssociatedObject?.GetVisualAncestor<ItemsControl>() is ItemsControl items))
64+
{
65+
return null;
66+
}
67+
68+
var states = (IDictionary<object, bool>)items.GetValue(ExpandedStateStoreProperty);
69+
70+
if (states != null)
71+
{
72+
return states;
73+
}
74+
75+
states = new Dictionary<object, bool>();
76+
items.SetValue(ExpandedStateStoreProperty, states);
77+
78+
return states;
79+
}
80+
}
81+
}

Rubberduck.Core/UI/GridViewSort.cs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
1+
//using System.Collections.Generic;
2+
//using System.Linq;
33

4-
namespace Rubberduck.UI
5-
{
6-
public class GridViewSort<T>
7-
{
8-
public bool SortedAscending { get; private set; }
9-
public string ColumnName { get; private set; }
4+
//namespace Rubberduck.UI
5+
//{
6+
// public class GridViewSort<T>
7+
// {
8+
// public bool SortedAscending { get; private set; }
9+
// public string ColumnName { get; private set; }
1010

11-
public GridViewSort(string columnName, bool sortedAscending)
12-
{
13-
ColumnName = columnName;
14-
SortedAscending = sortedAscending;
15-
}
11+
// public GridViewSort(string columnName, bool sortedAscending)
12+
// {
13+
// ColumnName = columnName;
14+
// SortedAscending = sortedAscending;
15+
// }
1616

17-
public IEnumerable<T> Sort(IEnumerable<T> items, string columnName)
18-
{
19-
if (columnName == ColumnName && SortedAscending)
20-
{
21-
SortedAscending = false;
22-
return items.OrderByDescending(x => x.GetType().GetProperty(columnName).GetValue(x));
23-
}
17+
// public IEnumerable<T> Sort(IEnumerable<T> items, string columnName)
18+
// {
19+
// if (columnName == ColumnName && SortedAscending)
20+
// {
21+
// SortedAscending = false;
22+
// return items.OrderByDescending(x => x.GetType().GetProperty(columnName).GetValue(x));
23+
// }
2424

25-
ColumnName = columnName;
26-
SortedAscending = true;
27-
return items.OrderBy(x => x.GetType().GetProperty(columnName).GetValue(x));
28-
}
25+
// ColumnName = columnName;
26+
// SortedAscending = true;
27+
// return items.OrderBy(x => x.GetType().GetProperty(columnName).GetValue(x));
28+
// }
2929

30-
public IEnumerable<T> Sort(IEnumerable<T> items, string columnName, bool sortAscending)
31-
{
32-
SortedAscending = sortAscending;
33-
ColumnName = columnName;
30+
// public IEnumerable<T> Sort(IEnumerable<T> items, string columnName, bool sortAscending)
31+
// {
32+
// SortedAscending = sortAscending;
33+
// ColumnName = columnName;
3434

35-
return sortAscending
36-
? items.OrderBy(x => x.GetType().GetProperty(columnName).GetValue(x))
37-
: items.OrderByDescending(x => x.GetType().GetProperty(columnName).GetValue(x));
38-
}
39-
}
40-
}
35+
// return sortAscending
36+
// ? items.OrderBy(x => x.GetType().GetProperty(columnName).GetValue(x))
37+
// : items.OrderByDescending(x => x.GetType().GetProperty(columnName).GetValue(x));
38+
// }
39+
// }
40+
//}

0 commit comments

Comments
 (0)