Applying a conditional effect to a Label

jholmejholme Member ✭✭

Hello! I am having quite a bit of trouble trying to add a conditional strikethrough effect to a label. The following generates an error "Can't resolved Effects on Label". I am trying to add a strikethrough effect to a label if the operation type is a certain value - otherwise it should just default to MyStyle.

I'm stuck and I would appreciate any tips! Thanks!

       <Label
                        Style="{StaticResource MyStyle}"
                        Text="{Binding Name}">
                        <Label.Triggers>
                            <DataTrigger
                                Binding="{Binding Name.Operation}"
                                TargetType="Label"
                                Value="2">
                                <Setter Property="Effects">
                                    <Setter.Value>
                                        <effects:StrikethoughEffect />
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                        </Label.Triggers>
                    </Label>
Tagged:

Best Answer

Answers

  • igorkr_10igorkr_10 Member ✭✭✭
    Label has a property TextDecorations where you can set strikethrough text. For effects you need to make sure that using of them is right. Check in documentation how to properly use effects.
  • igorkr_10igorkr_10 Member ✭✭✭
    Also I can't see a link between trigger and Style.
    Maybe its better to bind TextDecorations property of Label to property of ViewModel? And when your operation is changing just call OnPropertyChanged for that property?
  • jholmejholme Member ✭✭

    Thank you. However, I do not believe TextDecorations are in the version of Xamarin.Forms I am using (3.2.x).

  • JarvanJarvan Member, Xamarin Team Xamurai

    I've reproduced the code. There are no 'Effects' in Setter's Property.
    You can change the code as follows :

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:EffectsDemo" x:Class="EffectsDemo.HomePage">
        <StackLayout Padding="0,20,0,0">
            <Label Text="Effects Demo - Focus Effect" FontAttributes="Bold" HorizontalOptions="Center">
                <Label.Effects>
                    <local:FocusEffect />
                </Label.Effects>
        ...
                <Label.Triggers>
                    <DataTrigger TargetType="Label"
                                 Value="2">
                        <Setter Property="FontSize">
                            <Setter.Value>
                                24
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Label.Triggers>
            </Label>
        </StackLayout>
    </ContentPage>
    
Sign In or Register to comment.