How to remove on/off text from switch
<Label Text="Below is the binded data: "></Label>
<Label Text="{Binding MyData}"></Label>
<Label x:Name="lbldisp"></Label>
<Switch Toggled="SwitchToggled"></Switch>
How to remove on/off text from switch
The native control corresponding to the Switch
in UWP is ToggleSwitch
. If you want to remove on/off
content, you could create ToggleSwitch
without text style directly in UWP
project like following:
App.xaml
<Application.Resources>
<Style TargetType="ToggleSwitch">
<Setter Property="OffContent" Value=" " />
<Setter Property="OnContent" Value=" " />
<Setter Property="Margin" Value="0,0,-110,0" />
</Style>
</Application.Resources>
I used an Effect to solve this one.
In your UWP project;
using Windows.UI.Xaml.Controls;
using Xamarin.Forms;
using Xamarin.Forms.Platform.UWP;
[assembly: ResolutionGroupName("YOURAPP")]
[assembly: ExportEffect(typeof(YOURAPP.UWP.Effects.SwitchEffect), nameof(YOURAPP.UWP.Effects.SwitchEffect))]
namespace YOURAPP.UWP.Effects
{
public class SwitchEffect : PlatformEffect
{
protected override void OnAttached()
{
if (Control is ToggleSwitch switchControl)
{
switchControl.OffContent = string.Empty;
switchControl.OnContent = string.Empty;
}
}
protected override void OnDetached()
{ }
}
}
In your Forms project:
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace YOURAPP.Effects
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public class SwitchEffect : RoutingEffect
{
public SwitchEffect() : base("YOURAPP.SwitchEffect") { }
}
}
In your XAML: Add the namespace:
xmlns:effects="clr-namespace:YOURAPP.Effects;assembly=YOURAPP"
<Switch>
<Switch.Effects>
<effects:SwitchEffect />
</Switch.Effects>
</Switch>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With