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?

Posts

  • 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" />
          </Style>
    
  • RicardoMedinaRicardoMedina USMember ✭✭

    Hi Stacy,

    I have the same issue and tried your solution:

    ``
    <Application.Resources>

        <ResourceDictionary>
            <!-- 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}" />
            </Style>``
    

    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;
        }
    }
    
    // https://github.com/neilkennedy/FontAwesome.Xamarin/blob/master/FontAwesome.Xamarin/FontAwesome.cs
    // 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?

Sign In or Register to comment.