#22360·Avalonia

No clean way to set the margin, padding and border of the FluentTheme ToggleSwitch

Author: elgonzoCreated Sep 30, 2026Updated Sep 30, 2026
Labelsbug

Describe the bug

Not sure if the following constitutes a bug or a feature suggestion, but in the end i decided to file this as an issue report.

There currently seems to be no straightforward way to eliminate the margin around the ToggleSwitch in the Fluent theme and maintain proper sizing of the input focus decoration. Setting the Margin and the Padding properties on the ToggleSwitch to zero has no effect, neither are the BorderBrush and BorderThickness properties honored.

To Reproduce

With this simple XAML:

<Border Margin="5" BorderBrush="Red" BorderThickness="1" VerticalAlignment="Top">
        <ToggleSwitch x:Name="MyTB" Margin="0" Padding="0" VerticalAlignment="Top" BorderBrush="Green" BorderThickness="2" />
</Border>

i attempt to make the ToggleSwitch hug the surrounding Border while also trying to visualize the bounds of the ToggleSwitch with a fat green border.

This the result:

Image

Note that the ToggleSwitch is not hugging the Border parent. Neither is the green border being rendered.

Sleuthing around in the internet, i got the hint to override certain style resources to reduce the ToggleSwitch margin to zero. The updated XAML now is as follow:

<Window.Resources>
        <GridLength x:Key="ToggleSwitchPreContentMargin">0</GridLength>
        <GridLength x:Key="ToggleSwitchPostContentMargin">0</GridLength>
</Window.Resources>

<Border Margin="5" BorderBrush="Red" BorderThickness="1" VerticalAlignment="Top">
        <ToggleSwitch x:Name="MyTB" Margin="0" Padding="0" VerticalAlignment="Top" BorderBrush="Green" BorderThickness="2" />
</Border>

and looks like that:

Image

Still no fat green border in sight, but at least the ToggleSwith is hugging the red Border now. Looks like success. ...well, hold your horses. Press the TAB key to set the input focus on the ToggleSwitch, and this is result:

Image

The input focus decoration of the ToggleSwitch is now completely buggered. The cause of the issue seems to be a hard-coded margin of the SwitchAreaGrid grid in the ControlTheme of the ToggleSwitch: https://github.com/AvaloniaUI/Avalonia/blob/bc4d4396488b6b6b19b7fb81c74805cc84c12ac5/src/Avalonia.Themes.Fluent/Controls/ToggleSwitch.xaml#L68-L74

Trying to override this with a style like:

<Window.Styles>
    <Style Selector="ToggleSwitch /template/ Grid#SwitchAreaGrid">
        <Setter Property="Margin" Value="0" />
    </Style>
</Window.Styles>

has no effect. I have a vague suspicion that the style doesn't apply because there's no TemplatePartAttribute declared for "SwitchAreaGrid" in the ToggleSwitch.cs code-behind, but i don't know much about Avalonia's machinery to have a firmer suspicion.

So, to fix the wrongly sized input focus decoration, i need to rummage around in the visual tree to find the SwitchAreaGrid grid and set its margin to zero in code-behind:

protected override void OnLoaded(RoutedEventArgs e)
{
    base.OnLoaded(e);
    var g = (Grid?) ToggleAutoDownload.GetVisualDescendants().FirstOrDefault(c => (c as Grid)?.Name == "SwitchAreaGrid");
    g?.Margin = new Thickness(0);
}

Now, with both the resource overrides for ToggleSwitchPreContentMargin and ToggleSwitchPosContentMargin, as well as the hacking around in the visual tree, the ToggleSwitch and its input focus decoration render almost as desired:

Image

It's still not a full success, though. The ToggleSwitch's green border is missing, and note how the right edge of the input focus decoration lies over the last letter of the "Off" text - yet another layout issue with the Fluent theme for the toggle switch.

Also, the Padding property seems to be ineffectual (which would be of relevance if the BorderBrush and BorderThickness properties were to be honored, as they should).

Expected behavior

  1. Setting ToggleSwitch.Margin to zero should be sufficient to result in a visually corresponding zero margin, like:

    Image
  2. No overriding of theme resources should be required.

  3. User-specified ToggleSwitch.BorderBrush, ToggleSwitch.BorderThickness and ToggleSwitch.Padding values need to be respected and not be overruled/ignored by the respective ControlTemplate of a theme.

  4. Layout of the input focus decoration needs to be adjusted to not lie over the last letter of the text label.

Avalonia version

Avalonia 12.1.3, Avalonia.Themes.Fluent 12.1.3

OS

Windows

Additional context

No response