Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToggleSwitch control internally sets a MinWidth to 154, which is way larger than its default width #3652

Open
Tracked by #8762
jbrianceau opened this issue Nov 19, 2020 · 15 comments · Fixed by #4960
Labels
area-ToggleSwitch bug Something isn't working help wanted Issue ideal for external contributors team-Controls Issue for the Controls team

Comments

@jbrianceau
Copy link
Member

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 Width property of the ToggleSwitch control 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:
image

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

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Nov 19, 2020
@StephenLPeters
Copy link
Contributor

I tried to redefine ToggleSwitchThemeMinWidth resource in many places of my application to try to override it without success.

This is weird, overriding this resource should work from any parent of the toggle switch. Can you try doing it in your App.xaml?

The template for toggle switch does seem weird, there should probably be a setter in the style targetting the minwidth property that sets it to this and then we should template bind the toggle switches min width to this template part.

The reason that this was done is likely to maintain accessible compliant touch targets, you can see similar things with the checkbox and radio button templates, however they do the template binding thing I described above.

@StephenLPeters StephenLPeters added area-ToggleSwitch help wanted Issue ideal for external contributors team-Controls Issue for the Controls team and removed needs-triage Issue needs to be triaged by the area owners labels Nov 20, 2020
@jbrianceau
Copy link
Member Author

Can you try doing it in your App.xaml?

It also doesn't work unfortunately. Note that I've tried to perform the override before or after referencing <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" /> and it doesn't work both ways.

@marcelwgn
Copy link
Contributor

I would like to work on this. I think the @StephenLPeters proposed solution seems the best for this.

@mdtauk
Copy link
Contributor

mdtauk commented May 5, 2021

I would like to work on this. I think the @StephenLPeters proposed solution seems the best for this.

Is this a value affected by the localisation text for On and Off?

@marcelwgn
Copy link
Contributor

Is this a value affected by the localisation text for On and Off?

I don't think so.

@nkasco
Copy link

nkasco commented Mar 9, 2022

This still appears to be an issue, setting MinWidth to 0 does resolve it. Very odd and unexpected.

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Mar 9, 2022
@StephenLPeters
Copy link
Contributor

@nkasco are you setting the property on toggle switch or the resource ToggleSwitchThemeMinWidth?

@ghost ghost removed the needs-triage Issue needs to be triaged by the area owners label Mar 11, 2022
@nkasco
Copy link

nkasco commented Mar 11, 2022

@nkasco are you setting the property on toggle switch or the resource ToggleSwitchThemeMinWidth?

On the Toggle Switch XAML directly:
<ToggleSwitch Name="SettingStartupBehavior" HorizontalAlignment="Right" Grid.Column="1" OffContent="" OnContent="" MinWidth="0"/>

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Mar 11, 2022
@ojhad
Copy link
Contributor

ojhad commented Mar 14, 2022

@nkasco what version of WinUI are you using?

@ghost ghost removed the needs-triage Issue needs to be triaged by the area owners label Mar 14, 2022
@petercoin
Copy link

I've also found this issue with WindowsAppSDK v1.0.3.

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label May 19, 2022
@ojhad ojhad reopened this May 25, 2022
@ojhad ojhad removed the needs-triage Issue needs to be triaged by the area owners label May 25, 2022
@github-actions
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@mdtauk
Copy link
Contributor

mdtauk commented Jul 30, 2023

I think this is still an Issue.

@yki5000
Copy link

yki5000 commented Aug 17, 2023

Still an issue in 1.3.230724000.

@mibrito707
Copy link

To not have the default larger MinWidth is better to set it to 0, you can control the value in 2 ways:

  • overriding ToggleSwitchThemeMinWidth resource in any parent control, like in your App.xaml <x:Double x:Key="ToggleSwitchThemeMinWidth">0</x:Double>
  • or setting MinWidth directly in the ToogleSwitch <ToggleSwitch MinWidth="0"/>

@jeremy-visionaid
Copy link

Still an issue in Windows App SDK 1.4.230822000

@mibrito707 I find that ToggleSwitchThemeMinWidth has no effect, but setting MinWidth directly works OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-ToggleSwitch bug Something isn't working help wanted Issue ideal for external contributors team-Controls Issue for the Controls team
Projects
None yet
Development

Successfully merging a pull request may close this issue.