Skip to content

Commit c021af5

Browse files
committed
refactor: (gallery/TabViewPage) update closing handlers
1 parent b1fcbb9 commit c021af5

File tree

2 files changed

+40
-50
lines changed

2 files changed

+40
-50
lines changed

source/iNKORE.UI.WPF.Modern.Gallery/Pages/Controls/Windows/TabViewPage.xaml

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,46 +167,51 @@
167167
<local:ControlExample x:Name="Example6" HeaderText="A TabControl with closing handler">
168168
<ikw:SimpleStackPanel>
169169
<TextBlock TextWrapping="Wrap" Margin="0,0,0,12">
170-
The <Bold>TabControl</Bold> now supports intercepting tab close requests via the
171-
<Bold>TabItemCloseRequest</Bold> event. This allows preventing the close operation
172-
dynamically - for example, to prompt the user for confirmation or prevent closing under certain conditions.
173-
Simply handle the event and set <Bold>e.Cancel = true</Bold> to keep the tab open.
170+
The <Bold>TabControl</Bold> comes with a close button. To handle the close request,
171+
subscribe to the <Bold>TabCloseRequested</Bold> event. In the event handler, you should
172+
remove the tab from the TabControl's Items collection to close it if desired.
173+
Feel free to implement custom logic such as confirmation dialogs or saving states.
174+
<LineBreak/><LineBreak/>
175+
You can set <Bold>TabItem.(ui:TabItemHelper.IsClosable)</Bold> attached property to <Bold>False</Bold> to hide and disable
176+
the close button for specific tabs (or all tabs).
174177
</TextBlock>
175178

176179
<TabControl x:Name="tabControl6" MinHeight="160"
177180
ui:TabControlHelper.TabCloseRequested="tabControl6_TabCloseRequested">
178181
<TabItem x:Name="TabItem_Example6_Tab1" Header="Closable 1">
179182
<TextBlock TextWrapping="Wrap" Margin="12">
180-
This tab demonstrates the default close behavior. When the close button is clicked, the tab closes immediately
181-
without any interception or confirmation. TabItemCloseRequest handler will not do anything for this tab.
183+
This tab can be closed normally by clicking the close button. When the close button is clicked,
184+
codebehind will handle the <Bold>TabCloseRequested</Bold> event and remove this tab from the TabControl's
185+
Items collection.
182186
</TextBlock>
183187
</TabItem>
184188
<TabItem x:Name="TabItem_Example6_Tab2" Header="Closable 2">
185189
<TextBlock TextWrapping="Wrap" Margin="12">
186-
This tab demonstrates the default close behavior. When the close button is clicked, the tab closes immediately
187-
without any interception or confirmation. TabItemCloseRequest handler will not do anything for this tab.
190+
This tab can be closed normally by clicking the close button. When the close button is clicked,
191+
codebehind will handle the <Bold>TabCloseRequested</Bold> event and remove this tab from the TabControl's
192+
Items collection.
188193
</TextBlock>
189194
</TabItem>
190195
<TabItem x:Name="TabItem_Example6_Tab3" Header="Confirm To Close" Tag="ConfirmClose">
191196
<TextBlock TextWrapping="Wrap" Margin="12">
192-
This tab intercepts the close request using the <Bold>TabItemCloseRequest</Bold> event.
193-
When the close button is clicked, a confirmation dialog appears. If the user chooses to cancel,
194-
the tab remains open. This is useful for preserving unsaved work or preventing accidental closure.
197+
This tab requires confirmation before closing. When the close button is clicked,
198+
the <Bold>TabItemCloseRequest</Bold> event is handled in codebehind, prompting the user
199+
with a confirmation dialog. If the user confirms, the tab is closed; otherwise, the event
200+
handler will not do anything, leaving the tab open.
195201
</TextBlock>
196202
</TabItem>
197203
<TabItem x:Name="TabItem_Example6_Tab4" Header="DON'T TOUCH ME!" Tag="NiceTry">
198204
<TextBlock TextWrapping="Wrap" Margin="12">
199-
This tab is configured to reject all close requests. The <Bold>TabItemCloseRequest</Bold> event is handled
200-
and <Bold>e.Cancel</Bold> is always set to <Bold>true</Bold>, making the tab effectively non-closable.
201-
<LineBreak/>
202-
This is not usually recommended for user experience, but it demonstrates the flexibility of the event handling.
205+
This tab cannot be closed. When the close button is clicked, the <Bold>TabItemCloseRequest</Bold>
206+
handler doesn't do anything, preventing the tab from being closed.
203207
</TextBlock>
204208
</TabItem>
205209
<TabItem x:Name="TabItem_Example6_Tab5" Header="Unclosable (nobutt) "
206210
ui:TabItemHelper.IsClosable="False">
207211
<TextBlock TextWrapping="Wrap" Margin="12">
208-
This tab demonstrates the default close behavior. When the close button is clicked, the tab closes immediately
209-
without any interception or confirmation. TabItemCloseRequest handler will not do anything for this tab.
212+
This tab cannot be closed because the close button is hidden and disabled by setting
213+
<Bold>ui:TabItemHelper.IsClosable</Bold> attached property to <Bold>False</Bold>,
214+
thus, the close button is not shown.
210215
</TextBlock>
211216
</TabItem>
212217

source/iNKORE.UI.WPF.Modern.Gallery/Pages/Controls/Windows/TabViewPage.xaml.cs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -222,36 +222,45 @@ public void UpdateExampleCode()
222222
";
223223

224224
public string Example6Xaml => $@"
225-
<TabControl x:Name=""tabControl6"" ui:TabControlHelper.TabItemClosing=""tabControl6_TabItemClosing"">
226-
<TabItem x:Name=""TabItem_Example6_Tab1"" Header=""Closable""/>
227-
<TabItem x:Name=""TabItem_Example6_Tab2"" Header=""Confirm To Close"" Tag=""ConfirmClose""/>
228-
<TabItem x:Name=""TabItem_Example6_Tab3"" Header=""DON'T TOUCH ME!"" Tag=""NiceTry""/>
225+
<TabControl x:Name=""tabControl6"" ui:TabControlHelper.TabCloseRequested=""tabControl6_TabCloseRequested"">
226+
<TabItem x:Name=""TabItem_Example6_Tab1"" Header=""Closable 1"" />
227+
<TabItem x:Name=""TabItem_Example6_Tab2"" Header=""Closable 2"" />
228+
<TabItem x:Name=""TabItem_Example6_Tab3"" Header=""Confirm To Close"" Tag=""ConfirmClose"" />
229+
<TabItem x:Name=""TabItem_Example6_Tab4"" Header=""DON'T TOUCH ME!"" Tag=""NiceTry"" />
230+
<TabItem x:Name=""TabItem_Example6_Tab5"" Header=""Unclosable (nobutt) "" />
229231
</TabControl>
230232
";
231233

232234
public string Example6CS => $@"
233-
private void tabControl6_TabItemClosing(object sender, TabViewTabCloseRequestedEventArgs e)
235+
private void tabControl6_TabCloseRequested(object sender, TabViewTabCloseRequestedEventArgs e)
234236
{{
237+
var doClose = true;
238+
235239
if (e.Tab.Tag is ""ConfirmClose"")
236240
{{
237241
var msgResult = MessageBox.Show(""Do you want to close this tab?"", ""Confirm"", MessageBoxButton.OKCancel, MessageBoxImage.Question);
238242
if (msgResult != MessageBoxResult.OK)
239243
{{
240-
e.Cancel = true;
244+
doClose = false;
241245
return;
242246
}}
243247
}}
244248
else if (e.Tab.Tag is ""NiceTry"")
245249
{{
246-
e.Cancel = true;
250+
doClose = false;
247251
248252
if ((e.Tab.Header as string) != NiceTry)
249253
e.Tab.Header = NiceTry;
250254
else e.Tab.Header = ""You can't close me!"";
251255
252256
return;
253257
}}
254-
258+
259+
if (doClose && sender is TabControl tabControl)
260+
{{
261+
// Do remove the tab in order to close it.
262+
tabControl.Items.Remove(e.Tab);
263+
}}
255264
}}
256265
";
257266

@@ -268,30 +277,6 @@ private void IsClosableCheckBox_Click(object sender, RoutedEventArgs e)
268277

269278
#endregion
270279

271-
private void tabControl6_TabItemClosing(object sender, TabViewTabCloseRequestedEventArgs e)
272-
{
273-
//if (e.Tab.Tag is "ConfirmClose")
274-
//{
275-
// var msgResult = MessageBox.Show("Do you want to close this tab?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Question);
276-
// if (msgResult != MessageBoxResult.OK)
277-
// {
278-
// e.Cancel = true;
279-
// return;
280-
// }
281-
//}
282-
//else if (e.Tab.Tag is "NiceTry")
283-
//{
284-
// e.Cancel = true;
285-
286-
// if ((e.Tab.Header as string) != NiceTry)
287-
// e.Tab.Header = NiceTry;
288-
// else e.Tab.Header = "You can't close me!";
289-
290-
// return;
291-
//}
292-
293-
}
294-
295280
private void tabControl_TabCloseRequested(object sender, TabViewTabCloseRequestedEventArgs e)
296281
{
297282
MessageBox.Show($"You are closing tab: {e.Tab.Header}", "Tab Close Requested", MessageBoxButton.OK, MessageBoxImage.Information);
@@ -323,8 +308,8 @@ private void tabControl6_TabCloseRequested(object sender, TabViewTabCloseRequest
323308

324309
if (doClose && sender is TabControl tabControl)
325310
{
311+
// Do remove the tab in order to close it.
326312
tabControl.Items.Remove(e.Tab);
327-
328313
}
329314
}
330315
}

0 commit comments

Comments
 (0)