Forum Xamarin.Forms

Implicit Styles for Custom Controls

I have already tried this:

<Style TargetType="{x:Type local:CustomControl}"> <Setter Property="Padding" Value="20,40,20,0" /> </Style>

But it does not work. Is there something I missed?


  • MarcoTambaloMarcoTambalo PHMember ✭✭

    When using this syntax:

    <Style TargetType="{local:CustomControl}"> <Setter Property="Padding" Value="20,40,20,0" /> </Style>

    It seems to instantiate the custom control because the constructor is invoked, but throw an InvalidCastException.
    Any ideas?

  • StacyMahnke.2763StacyMahnke.2763 USMember ✭✭
    edited February 2017

    I know this is an older thread, but in case you haven't found the answer.

    First reference the namespace properly in the Application root (IE: xmlns:ctrls="clr-namespace:ProjectName.Folder")

    Then use the below:

           <Style TargetType="ctrls:CustomControlName">
              <Setter Property="TextColor" Value="Black" />
  • RicardoMedinaRicardoMedina USMember ✭✭

    Hi Stacy,

    I have the same issue and tried your solution:


            <!-- Pallete -->
            <Color x:Key="nav-title-bg_color">#26506b</Color>
            <Color x:Key="nav-title_color">#ffffff</Color>
            <Color x:Key="topmenu-name_color">#81d7ed</Color>
            <Color x:Key="topmenu-account_color">#5fb1c6</Color>
            <!-- Styles -->
            <Style x:Key="SideMenuNameIcon" TargetType="ctrls:FontAwesomeLabel">
                <Setter Property="Margin" Value="10,10,10,35" />
                <Setter Property=" HorizontalOptions" Value="Start" />
                <Setter Property="VerticalOptions" Value="End" />
                <Setter Property="FontSize" Value="15" />
                <Setter Property="TextColor" Value="{StaticResource topmenu-name_color}" />

    The Control:

    `namespace AAAApp.UserControls
    public class FontAwesomeLabel : Label
    public static readonly string FontAwesomeName = "FontAwesome";

        //Parameterless constructor for XAML
        public FontAwesomeLabel()
            FontFamily = FontAwesomeName;
        public FontAwesomeLabel(string fontAwesomeLabel = null)
            FontFamily = FontAwesomeName;
            Text = fontAwesomeLabel;
    // For a huge list of icon codes
    public static class Icon
        public static readonly string FAGlass = "\uf000";
        public static readonly string FAMusic = "\uf001";`

    But is still breaks. Any Suggestions?

  • SagarPanwalaSagarPanwala USMember ✭✭✭

    @RicardoMedina : Did you find solution to this ? I'm also facing the same issue.

  • RicardoMedinaRicardoMedina USMember ✭✭

    Sorry Sagar, I know I did, but don't remember how. Haven't done a mobile app on xamarin for a while. But this may help:

Sign In or Register to comment.