Can data triggers be used in external style

65 views Asked by At

I just started out with MAUI and I want to apply a style that will be used on more than 1 page with data triggers. Here is what I'm trying to accomplish:

TestPage contains a label like this:

<Label
    Text="{Binding TaskStatus}"
    VerticalOptions="Center"
    HorizontalOptions="Center"
    Grid.Row="0"
    Grid.Column="0"
    Style="{StaticResource DefaultLabelStyle}" />

And then the DefaultStyleResource file contains this:

<Style x:Key="DefaultLabelStyle"
    TargetType="Label">
    <Style.Triggers>            
        <DataTrigger
            TargetType="Label"
            Binding="{Binding TaskStatus}"
            Value="4">
            <Setter
                Property="TextColor"
                Value="{DynamicResource Blue}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Is this possible or can I only use data triggers like this in an inline manner? Thanks in advance!

1

There are 1 answers

0
Sean He-MSFT On BEST ANSWER

Styles can be defined globally by adding them to the app's resource dictionary. You can add the Style code to App.xaml file:

<Application.Resource>
    <ResourceDictionary>
        ...
        <Style x:Key="DefaultLabelStyle"> ...
     </ResourceDictionary>
</Application.Resource>

After this, you can use the style in your pages directly:

<Label
    Text="{Binding TaskStatus}"
    Style="{StaticResource DefaultLabelStyle}" 
    .../>

For more information, you can refer to the Global styles.