Xamarin.Forms : Using styles on custom UserControls

To use styles for your custom usercontrol, you have to define the styles TargetType property with an x:Type in your resource dictionary.

<ResourceDictionary
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:buttons="clr-namespace:AnAwesomeApp.Controls.Buttons"
    x:Class="AnAwesomeApp.Resources.Styles">

Declare the clr-namespace where your custom usercontrol lives.

<Style
        x:Key="BlueButtonStyle"
        TargetType="{x:Type buttons:ColoredButton}">
        <Setter
            Property="TextColor"
            Value="White" />
        <Setter
            Property="Color"
            Value="{AppThemeBinding Dark=#2325A6, Light=#61BAFF}" />
        <Setter
            Property="FontFamily"
            Value="Nunito" />
    </Style>

Set the target type with x:Type.

And boom, done!

This content has 3 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

1 thought on “Xamarin.Forms : Using styles on custom UserControls”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.