Skip to content

Commit 6f1309c

Browse files
authored
various improvements (#5123)
1 parent dc90a88 commit 6f1309c

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

hub/apps/design/controls/tab-view.md

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ms.localizationpriority: medium
1212

1313
The TabView control is a way to display a set of tabs and their respective content. TabView controls are useful for displaying several pages (or documents) of content while letting a user rearrange, close, or open new tabs.
1414

15-
![Example of a TabView](images/tabview/tab-introduction.png)
15+
:::image type="content" source="images/tabview/tab-introduction.png" alt-text="Example of a TabView":::
1616

1717
## Is this the right control?
1818

@@ -111,11 +111,11 @@ For each TabViewItem, you can set a header and an icon, and specify whether the
111111

112112
- The [Header](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.tabviewitem.header) property is typically set to a string value that provides a descriptive label for the tab. However, the `Header` property can be any object. You can also use the [HeaderTemplate](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.tabviewitem.headertemplate) property to specify a [DataTemplate](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.datatemplate) that defines how bound header data should be displayed.
113113
- Set the [IconSource](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.tabviewitem.iconsource) property to specify an icon for the tab.
114-
- By default, the tab shows a close button. You can set the [IsClosable](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.tabviewitem.isclosable) property to `false` to ensure that a user can't close the tab.
114+
- By default, the tab shows a _close button_ (X). You can set the [IsClosable](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.tabviewitem.isclosable) property to `false` to hide the close button and ensure that a user can't close the tab. (If you close tabs in your app code outside of a _close requested_ event, you should first check that `IsClosable` is `true`.)
115115

116116
For the TabView, you can configure several options that apply to all tabs.
117117

118-
- By default, the _close button_ (X) is always shown for closable tabs. You can set the [CloseButtonOverlayMode](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.tabview.closebuttonoverlaymode) property to `OnPointerOver` to change this behavior. In this case, the selected tab always shows the close button if it is closable, but unselected tabs show the close button only when the tab is closable and the user has their pointer over it.
118+
- By default, the close button is always shown for closable tabs. You can set the [CloseButtonOverlayMode](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.tabview.closebuttonoverlaymode) property to `OnPointerOver` to change this behavior. In this case, the selected tab always shows the close button if it is closable, but unselected tabs show the close button only when the tab is closable and the user has their pointer over it.
119119
- You can set the [TabWidthMode](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.tabview.tabwidthmode) property to change how tabs are sized. (The `Width` property is ignored on `TabViewItem`.) These are the options in the [TabViewWidthMode](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.tabviewwidthmode) enumeration:
120120
- `Equal` - Each tab has the same width. This is the default.
121121
- `SizeToContent` - Each tab adjusts its width to the content within the tab.
@@ -225,15 +225,17 @@ private void TabView_TabCloseRequested(TabView sender,
225225

226226
If all tabs in your app are closeable and your app window should close when the last tab is closed, you should also close the window in the [TabCloseRequested](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.tabview.tabcloserequested) event handler.
227227

228-
First, in the `App.xaml.cs` file, add a public property that will let you access the `Window` instance from the `Page` that hosts the TabView.
228+
First, in the `App.xaml.cs` file, add a `public static` property that will let you access the `Window` instance from the `Page` that hosts the TabView. (See [User interface migration](../../windows-app-sdk/migrate-to-windows-app-sdk/guides/winui3.md#change-windowsuixamlwindowcurrent-to-appwindow).)
229229

230230
```csharp App.xaml.cs
231231
public partial class App : Application
232232
{
233233
// ... code removed.
234234
235-
public Window? Window => m_window; // Add this.
236-
private Window? m_window;
235+
// Add this.
236+
public static Window Window { get { return m_window; } }
237+
// Update this to make it static.
238+
private static Window m_window;
237239
}
238240
```
239241

@@ -249,8 +251,7 @@ private void TabView_TabCloseRequested(TabView sender,
249251

250252
if (sender.TabItems.Count == 0)
251253
{
252-
var window = (Application.Current as App)?.Window as MainWindow;
253-
window?.Close();
254+
App.Window.Close();
254255
}
255256
}
256257
```
@@ -486,7 +487,7 @@ Because a user can drag a window by its title bar to reposition the Window, it i
486487

487488
For more information, see [Title bar customization](../../develop/title-bar.md)
488489

489-
![Tabs in title bar](images/tabview/tab-extend-to-title.png)
490+
:::image type="content" source="images/tabview/tab-extend-to-title.png" alt-text="Tabs in title bar":::
490491

491492
```xaml
492493
<TabView VerticalAlignment="Stretch">
@@ -505,15 +506,14 @@ For more information, see [Title bar customization](../../develop/title-bar.md)
505506
```csharp
506507
private void MainPage_Loaded(object sender, RoutedEventArgs e)
507508
{
508-
var currentWindow = (Application.Current as App)?.Window as MainWindow;
509-
currentWindow.ExtendsContentIntoTitleBar = true;
510-
currentWindow.SetTitleBar(CustomDragRegion);
509+
App.Window.ExtendsContentIntoTitleBar = true;
510+
App.Window.SetTitleBar(CustomDragRegion);
511511
CustomDragRegion.MinWidth = 188;
512512
}
513513
```
514514

515515
> [!NOTE]
516-
> How you get a reference to the window (`currentWindow`) may vary depending on how you track windows in your app. For more information, see [Close the window when the last tab is closed](#close-the-window-when-the-last-tab-is-closed) and [Create and track a new window](#create-and-track-a-new-window) in this article.
516+
> How you get a reference to the window may vary depending on how you track windows in your app. For more information, see [Close the window when the last tab is closed](#close-the-window-when-the-last-tab-is-closed) and [Create and track a new window](#create-and-track-a-new-window) in this article.
517517
518518
## Keyboard guidance for developers
519519

@@ -573,7 +573,7 @@ private void NewTabKeyboardAccelerator_Invoked(KeyboardAccelerator sender,
573573
{
574574
// Create new tab.
575575
TabView senderTabView = (TabView)args.Element;
576-
if (senderTabView != null)
576+
if (senderTabView is not null)
577577
{
578578
// (Click handler defined in previous example.)
579579
TabView_AddTabButtonClick(senderTabView, new EventArgs());
@@ -586,15 +586,28 @@ private void CloseSelectedTabKeyboardAccelerator_Invoked(KeyboardAccelerator sen
586586
{
587587
TabView tabView = (TabView)args.Element;
588588
TabViewItem tab = (TabViewItem)tabView.SelectedItem;
589+
if (tab is not null)
590+
{
591+
CloseSelectedTab(tabView, tab);
592+
}
593+
args.Handled = true;
594+
}
595+
596+
private void TabView_TabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args)
597+
{
598+
CloseSelectedTab(sender, args.Tab);
599+
}
600+
601+
private void CloseSelectedTab(TabView tabView, TabViewItem tab)
602+
{
589603
// Only remove the selected tab if it can be closed.
590-
if (tabView is not null &&
591-
tab.IsClosable == true)
604+
if (tab.IsClosable == true)
592605
{
593606
tabView.TabItems.Remove(tab);
594607
}
595-
args.Handled = true;
596608
}
597609

610+
598611
private void NavigateToNumberedTabKeyboardAccelerator_Invoked(KeyboardAccelerator sender,
599612
KeyboardAcceleratorInvokedEventArgs args)
600613
{

0 commit comments

Comments
 (0)