Alan2
Alan2

Reputation: 24602

Can I have a default style for labels on a page with Xamarin.Forms?

In this Xaml code that I insert into another page on my app I have this code:

<?xml version="1.0" encoding="UTF-8"?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      xmlns:template="clr-namespace:Japanese.Templates" 
      x:Class="Japanese.Views.Phrases.Xaml.PhraseQuizInfo" 
      VerticalOptions="FillAndExpand" Padding="20,0,20,0">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="33*" />
        <ColumnDefinition Width="33*" />
        <ColumnDefinition Width="33*" />
    </Grid.ColumnDefinitions>
    <Label Grid.Column="0" Text="{Binding Deck}" Style="{StaticResource StatusLabel}" />
    <Label Grid.Column="1" Text="{Binding Cards}" Style="{StaticResource StatusLabel}" />
    <Label Grid.Column="2" Text="{Binding Timer}" Style="{StaticResource StatusLabel}" />
</Grid>

Is it possible to specify that all labels inside the Grid using the StaticResource called StatusLabel without having to add that to each label?

Upvotes: 1

Views: 711

Answers (1)

SushiHangover
SushiHangover

Reputation: 74209

There are explicit or implicit global styles.

Currently you are using an explicit style since you are defining a key for it and assigning them on each Label via their Style parameter.

<Style x:Key="StatusLabel" TargetType="Label">
   ~~~
</Style>

You can define an implicit style and thus ALL labels will receive that style, just do not define a key for it and do not manually assign the Style parameter

<Style TargetType="Label">
   ~~~
</Style>

You can then override individual implicit label's style via an explicit one.

So if the majority of your Labels are "StatusLabel" style, define that as the implicit style and override it when needed.

Re: Creating a Global Style in XAML

Note: Also you can define styles at the Page level and at the Control level and thus through Style inheritance can tweak individual style properties when needed.

Re: Style Inheritance

Upvotes: 3

Related Questions