7

I have a switch control in .Net Maui :

<Switch IsToggled="true" Grid.Row="3" Grid.Column="1" VerticalOptions="Center"/>

By default it shows a label "on/off" based on it's state. I want it to show nothing.

Is there any way to hide/remove it?

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Thomas Carlton
  • 5,344
  • 10
  • 63
  • 126
  • 1
    Search https://github.com/dotnet/maui/issues to see if there is any discussion of this. [OPINION] That's a bizarre choice for default behavior. Maybe that's WinUI's default (?), but WinUI also has `OffContent` and `OnContent` properties, that I see are missing in Maui. Lacking those, Maui should suppress the text. – ToolmakerSteve Aug 06 '22 at 19:43

2 Answers2

7

Unfortunately the Maui switch control have SwitchOnVisualState \ SwitchOffVisualState content set to constant strings and you can't override it yet apparently.

You can however override the ToggleSwitch class style in WinUI through the App.xaml file.

Project\Platforms\Windows\App.xaml

It should look like this.

 <maui:MauiWinUIApplication
      x:Class="Project.Client.WinUI.App"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:maui="using:Microsoft.Maui"
      xmlns:local="using:Project.Client.WinUI">
      <maui:MauiWinUIApplication.Resources>
      <ResourceDictionary>
        <!-- ToggleSwitch : Turn off ON/OFF label -->
        <Style TargetType="ToggleSwitch">
            <Setter Property="OffContent" Value=" " />
            <Setter Property="OnContent" Value=" " />
            <Setter Property="Margin" Value="0,0,-110,0" />
        </Style>
    </ResourceDictionary>
</maui:MauiWinUIApplication.Resources> 
</maui:MauiWinUIApplication>

See?

enter image description here

michael g
  • 603
  • 7
  • 14
  • doesnt work as of now, "cant resolve type ToggleSwitch". If changed to "Switch" -- "cant resolve property "OffContent" – Mike Tsayper Mar 28 '23 at 08:32
3

Please check the solution for this issue here https://github.com/dotnet/maui/issues/6177 I have tried one solution and it is working I have used Absolute Layout. As Absolute Layout gives you option to specify child's width . I have given 60 as width for the Switch controll. And it show only the Switch not On/Off text next to it. And also checked by resizing the window , all is working fine. The On/Off Text doesn't Appear.