ToggleSwitch control internally sets a `MinWidth` to 154, which is way larger than its default width
#3,652 创建于 2020年11月19日
描述
Describe the bug
The ToggleSwitch control template sets the MinWidth of one of its internal Grid to 154, through the following resource:
<x:Double x:Key="ToggleSwitchThemeMinWidth">154</x:Double>
Because of that, if you put a ToggleSwitch control in a column of a Grid having its ColumnDefinition width set to Auto, the column won't adjust to the size of the ToggleSwitch by default.
I tried to redefine ToggleSwitchThemeMinWidth resource in many places of my application to try to override it without success.
Note that this issue can be mitigated with one of the following workarounds, but this is not ideal:
- Set the
Widthproperty of theToggleSwitchcontrol to a fixed value - Retemplate the whole
ToggleSwitch
Steps to reproduce the bug For instance, put the following XAML code in a sample page:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="Here is a TextBlock" />
<ToggleSwitch Grid.Column="1" />
</Grid>
Expected behavior
I would expect the ToggleSwitch control to be aligned on the right of the grid.
Screenshots
Instead, there is a lot of wasted space on the right, as you can see in the following screenshot:

Version Info
NuGet package version: [Microsoft.UI.Xaml 2.4.3]
Windows app type:
| UWP | Win32 |
|---|---|
| Yes |
| Windows 10 version | Saw the problem? |
|---|---|
| Insider Build (xxxxx) | |
| 20H2 (19042) | Yes |
| May 2020 Update (19041) | |
| November 2019 Update (18363) | |
| May 2019 Update (18362) | |
| October 2018 Update (17763) | |
| April 2018 Update (17134) | |
| Fall Creators Update (16299) | |
| Creators Update (15063) |
| Device form factor | Saw the problem? |
|---|---|
| Desktop | Yes |
| Xbox | |
| Surface Hub | |
| IoT |
Additional context