-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathTabViewPage.xaml.cs
More file actions
366 lines (314 loc) · 12.3 KB
/
TabViewPage.xaml.cs
File metadata and controls
366 lines (314 loc) · 12.3 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using iNKORE.UI.WPF.Modern.Common.IconKeys;
using iNKORE.UI.WPF.Modern.Controls;
using iNKORE.UI.WPF.Modern.Controls.Helpers;
using iNKORE.UI.WPF.Modern.Controls.Primitives;
using SamplesCommon.SamplePages;
using Frame = iNKORE.UI.WPF.Modern.Controls.Frame;
using MessageBox = iNKORE.UI.WPF.Modern.Controls.MessageBox;
namespace iNKORE.UI.WPF.Modern.Gallery.Pages.Controls.Windows
{
public partial class TabViewPage
{
private readonly System.Collections.Generic.HashSet<TabControl> _setupControls = new System.Collections.Generic.HashSet<TabControl>();
public TabViewPage()
{
InitializeComponent();
// Seed the primary demo TabControls with initial tabs.
for (int i = 0; i < 3; i++)
{
tabControl.Items.Add(CreateNewTab(i));
tabControl2.Items.Add(CreateNewTab(i));
tabControl3.Items.Add(CreateNewTab(i));
}
InitializeExample6();
UpdateExampleCode();
}
private void TabView_Loaded(object sender, RoutedEventArgs e)
{
if (sender is not TabControl loadedTabControl) return;
if (_setupControls.Contains(loadedTabControl))
return;
// Hook add button (if visible) once.
var events = TabControlHelper.GetTabControlHelperEvents(loadedTabControl);
events.AddTabButtonClick += TabControl_AddButtonClick;
events.TabCloseRequested += TabControl_TabCloseRequested;
_setupControls.Add(loadedTabControl);
// Only auto-seed for dynamically loaded examples (avoid duplicating those we seeded in ctor)
if (loadedTabControl != tabControl && loadedTabControl != tabControl2 && loadedTabControl != tabControl3)
{
for (int i = 0; i < 3; i++)
{
loadedTabControl.Items.Add(CreateNewTab(i));
}
}
}
private void TabControl_AddButtonClick(TabControl sender, object args)
{
sender.Items.Add(CreateNewTab(sender.Items.Count));
sender.SelectedIndex = sender.Items.Count - 1;
}
private void TabControl_TabCloseRequested(TabControl sender, TabViewTabCloseRequestedEventArgs args)
{
sender.Items.Remove(args.Item);
}
private TabItem CreateNewTab(int index)
{
TabItem newItem = new TabItem();
newItem.Header = $"Document {index}";
TabItemHelper.SetIcon(newItem, new FontIcon(SegoeFluentIcons.Document));
// The content of the tab is often a frame that contains a page, though it could be any UIElement.
Frame frame = new Frame();
frame.Navigated += (s, e) =>
{
((FrameworkElement)frame.Content).Margin = new Thickness(-18, 0, -18, 0);
};
switch (index % 3)
{
case 0:
frame.Navigate(typeof(SamplePage1));
break;
case 1:
frame.Navigate(typeof(SamplePage2));
break;
case 2:
frame.Navigate(typeof(SamplePage3));
break;
}
newItem.Content = frame;
return newItem;
}
private void ShowHeaderAndFooterCheckBox_Click(object sender, RoutedEventArgs e)
{
UpdateExampleCode();
}
private void RadioButtons_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
UpdateExampleCode();
}
#region Example 6
private const string NiceTry = "Nice try!";
private void InitializeExample6()
{
ResetExample6Tabs();
tabControl.RegisterTabClosingEvent((menuControl, e) =>
{
if (e.Tab.Tag is "ConfirmClose")
{
var msgResult = MessageBox.Show("Do you want to close this tab?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Question);
if (msgResult != MessageBoxResult.OK)
{
e.Cancel = true;
return;
}
}
else if (e.Tab.Tag is "NiceTry")
{
e.Cancel = true;
if ((e.Tab.Header as string) != NiceTry)
e.Tab.Header = NiceTry;
else e.Tab.Header = "You can't close me!";
return;
}
});
}
private void ResetExample6Tabs()
{
tabControl6.Items.Clear();
tabControl6.Items.Add(TabItem_Example6_Tab1);
tabControl6.Items.Add(TabItem_Example6_Tab2);
tabControl6.Items.Add(TabItem_Example6_Tab3);
TabItem_Example6_Tab3.Header = "DON'T TOUCH ME!";
}
private void Button_Example6_ResetTabs_Click(object sender, RoutedEventArgs e)
{
ResetExample6Tabs();
}
#endregion
#region Example Code
public void UpdateExampleCode()
{
if (!this.IsInitialized) return;
Example1.Xaml = Example1Xaml;
Example1.CSharp = Example1CS;
Example2.Xaml = Example2Xaml;
Example3.Xaml = Example3Xaml;
Example4.Xaml = Example4Xaml;
Example5.Xaml = Example5Xaml;
Example6.Xaml = Example6Xaml;
Example6.CSharp = Example6CS;
}
public string Example1Xaml => $@"
<TabControl x:Name=""tabControl""
TabStripPlacement=""{tabControl.TabStripPlacement}"">
<ui:TabControlHelper.TabStripHeader>
<Button
HorizontalAlignment=""Stretch""
VerticalAlignment=""Stretch""
ui:FocusVisualHelper.FocusVisualMargin=""0""
Content=""Header""
Visibility=""{(ShowHeaderAndFooterCheckBox.IsChecked == true ? Visibility.Visible : Visibility.Collapsed)}"" />
</ui:TabControlHelper.TabStripHeader>
<ui:TabControlHelper.TabStripFooter>
<Button
HorizontalAlignment=""Stretch""
VerticalAlignment=""Stretch""
ui:FocusVisualHelper.FocusVisualMargin=""0""
Content=""Footer""
Visibility=""{(ShowHeaderAndFooterCheckBox.IsChecked == true ? Visibility.Visible : Visibility.Collapsed)}"" />
</ui:TabControlHelper.TabStripFooter>
</TabControl>
";
public string Example1CS => $@"
private void TabView_Loaded(object sender, RoutedEventArgs e)
{{
var loadedTabControl = (TabControl)sender;
var events = TabControlHelper.GetTabControlHelperEvents(loadedTabControl);
// hook up add & close
events.AddTabButtonClick += TabControl_AddButtonClick;
events.TabCloseRequested += TabControl_TabCloseRequested;
// seed initial 3 tabs
for (int i = 0; i < 3; i++)
loadedTabControl.Items.Add(CreateNewTab(i));
}}
private void TabControl_AddButtonClick(TabControl sender, object args)
{{
sender.Items.Add(CreateNewTab(sender.Items.Count));
sender.SelectedIndex = sender.Items.Count - 1;
}}
private void TabControl_TabCloseRequested(TabControl sender, TabControlTabCloseRequestedEventArgs args)
{{
sender.Items.Remove(args.Item);
}}
private TabItem CreateNewTab(int index)
{{
var newItem = new TabItem
{{
Header = $""Document {{index}}""
}};
TabItemHelper.SetIcon(newItem, new FontIcon(SegoeFluentIcons.Document));
var frame = new Frame();
frame.Navigated += (s, e) =>
{{
((FrameworkElement)frame.Content).Margin = new Thickness(-18, 0, -18, 0);
}};
switch (index % 3)
{{
case 0: frame.Navigate(typeof(SamplePage1)); break;
case 1: frame.Navigate(typeof(SamplePage2)); break;
case 2: frame.Navigate(typeof(SamplePage3)); break;
}}
newItem.Content = frame;
return newItem;
}}
";
public string Example2Xaml => $@"
<TabControl x:Name=""TabView4""
SelectedIndex=""0"">
<TabControl.Items>
<TabItem Header=""CMD Prompt"">
<ui:TabItemHelper.Icon>
<ui:BitmapIcon ShowAsMonochrome=""False"" UriSource=""/Assets/TabViewIcons/cmd.png"" />
</ui:TabItemHelper.Icon>
</TabItem>
<TabItem Header=""Powershell"">
<ui:TabItemHelper.Icon>
<ui:BitmapIcon ShowAsMonochrome=""False"" UriSource=""/Assets/TabViewIcons/powershell.png"" />
</ui:TabItemHelper.Icon>
</TabItem>
<TabItem Header=""Windows Subsystem for Linux"">
<ui:TabItemHelper.Icon>
<ui:BitmapIcon ShowAsMonochrome=""False"" UriSource=""/Assets/TabViewIcons/linux.png"" />
</ui:TabItemHelper.Icon>
</TabItem>
</TabControl.Items>
</TabControl>
";
public string Example3Xaml => $@"
<TabControl SelectedIndex=""0"">
<ui:TabControlHelper.TabStripHeader>
<TextBlock
Margin=""8,6""
VerticalAlignment=""Center""
Style=""{{DynamicResource BaseTextBlockStyle}}""
Text=""TabStripHeader Content"" />
</ui:TabControlHelper.TabStripHeader>
<ui:TabControlHelper.TabStripFooter>
<TextBlock
Margin=""6""
HorizontalAlignment=""Right""
VerticalAlignment=""Center""
Style=""{{DynamicResource BaseTextBlockStyle}}""
Text=""TabStripFooter Content"" />
</ui:TabControlHelper.TabStripFooter>
</TabControl>
";
public string Example4Xaml => $@"
<TabControl x:Name=""tabControl3"" ui:ThemeManager.HasThemeResources=""True"">
<TabControl.Resources>
<ui:ResourceDictionaryEx>
<ui:ResourceDictionaryEx.ThemeDictionaries>
<ResourceDictionary x:Key=""Light"">
<SolidColorBrush x:Key=""TabViewBackground"" Color=""{{DynamicResource SystemAccentColorLight2}}"" />
</ResourceDictionary>
<ResourceDictionary x:Key=""Dark"">
<SolidColorBrush x:Key=""TabViewBackground"" Color=""{{DynamicResource SystemAccentColorDark2}}"" />
</ResourceDictionary>
</ui:ResourceDictionaryEx.ThemeDictionaries>
</ui:ResourceDictionaryEx>
</TabControl.Resources>
</TabControl>
";
public string Example5Xaml => $@"
<TabControl x:Name=""tabControl2"">
<TabControl.Resources>
<sys:Double x:Key=""TabViewItemHeaderFontSize"">24</sys:Double>
<sys:Double x:Key=""TabViewItemHeaderIconSize"">32</sys:Double>
</TabControl.Resources>
<TabControl.ItemContainerStyle>
<Style BasedOn=""{{StaticResource DefaultTabItemStyle}}"" TargetType=""TabItem"">
<Setter Property=""FontFamily"" Value=""Courier New"" />
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
";
public string Example6Xaml => $@"
<TabControl x:Name=""tabControl6"">
<TabItem x:Name=""TabItem_Example6_Tab1"" Header=""Closable""/>
<TabItem x:Name=""TabItem_Example6_Tab2"" Header=""Confirm To Close"" Tag=""ConfirmClose""/>
<TabItem x:Name=""TabItem_Example6_Tab3"" Header=""DON'T TOUCH ME!"" Tag=""NiceTry""/>
</TabControl>
";
public string Example6CS => $@"
// Invoked during the constructor after InitializeComponent or at any later time
private void InitializeExample6()
{{
ResetExample6Tabs();
tabControl.RegisterTabClosingEvent((menuControl, e) =>
{{
if (e.Tab.Tag is ""ConfirmClose"")
{{
var msgResult = MessageBox.Show(""Do you want to close this tab?"", ""Confirm"", MessageBoxButton.OKCancel, MessageBoxImage.Question);
if (msgResult != MessageBoxResult.OK)
{{
e.Cancel = true;
return;
}}
}}
else if (e.Tab.Tag is ""NiceTry"")
{{
e.Cancel = true;
if ((e.Tab.Header as string) != NiceTry)
e.Tab.Header = NiceTry;
else e.Tab.Header = ""You can't close me!"";
return;
}}
}});
}}
";
#endregion
}
}