-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTabControlAdapter.cs
More file actions
105 lines (92 loc) · 3.67 KB
/
TabControlAdapter.cs
File metadata and controls
105 lines (92 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Specialized;
using System.Linq;
using Avalonia.Controls;
using Prism.Navigation.Regions;
namespace SampleApp.Common;
/// <summary>
/// Adapts TabControl's TabItem (content control) to a Prism Region
/// so that you can hook Regions to the TabControl in XAML.
/// <code><![CDATA[
/// <TabControl prism:RegionManager.RegionName="MailTabRegion" />
/// ]]></code>
///
/// Tab Control Adapter for hooking tabs to regions. a UserControl as a TabItem
/// * Tab Header: UserControl's `Tag` property
/// </summary>
public class TabControlAdapter : RegionAdapterBase<TabControl>
{
public TabControlAdapter(IRegionBehaviorFactory regionBehaviorFactory) : base(regionBehaviorFactory)
{
}
protected override void Adapt(IRegion region, TabControl regionTarget)
{
if (region == null)
throw new ArgumentNullException(nameof(region));
if (regionTarget == null)
throw new ArgumentNullException(nameof(regionTarget));
// Detect a Tab Selection Changed
regionTarget.SelectionChanged += (object s, SelectionChangedEventArgs e) =>
{
// The view navigating away from
foreach (var item in e.RemovedItems)
{
// NOTE: The selected item isn't always a TabItem, if the region contains
// a ListBox, it's SelecitonChange gets picked up.
TargetSelectionChanged("Deactivating", item);
//// region.Deactivate(item);
}
// The view navigating to
foreach (var item in e.AddedItems)
{
TargetSelectionChanged("Activating", item);
////region.Activate(item);
}
};
// Detect when a TabItem has been added/removed to the TabControl
region.Views.CollectionChanged += (s, e) =>
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (UserControl item in e.NewItems)
{
//var items = regionTarget.Items.Cast<TabItem>().ToList();
// items.Add(new TabItem { Header = item.Tag, Content = item });
// regionTarget.Items = items; // Avalonia v0.10.x
regionTarget.Items.Add(new TabItem { Header = item.Tag, Content = item });
//regionTarget.ItemsSource = items; // Avalonia v11.0
}
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (UserControl item in e.OldItems)
{
var tabToDelete = regionTarget.Items.OfType<TabItem>().FirstOrDefault(n => n.Content == item);
regionTarget.Items.Remove(tabToDelete);
// regionTarget.Items.Remove(tabToDelete); // WPF
//var items = regionTarget.Items.Cast<TabItem>().ToList();
// items.Remove(tabToDelete);
// regionTarget.Items = items; // Avalonia v0.10
//regionTarget.ItemsSource = items; // Avalonia v11
}
}
};
}
/// <summary>
/// AllActiveRegion - Can have multiple active views at the same time (i.e. multi-window views)
/// SingleActiveRegion - There is only one view active at a time. (i.e. Tab)
/// </summary>
/// <returns>Region</returns>
protected override IRegion CreateRegion() => new SingleActiveRegion();
private void TargetSelectionChanged(string changeAction, object itemChanged)
{
// The selected item isn't always a TabItem.
// In some cases, it could be the Region's ListBox item
TabItem item = itemChanged as TabItem;
if (item is null)
return;
System.Diagnostics.Debug.WriteLine($"Tab {changeAction} (Header): " + item.Header);
System.Diagnostics.Debug.WriteLine($"Tab {changeAction} (View): " + item.Content);
System.Diagnostics.Debug.WriteLine($"Tab {changeAction} (ViewModel): " + item.DataContext);
}
}