Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Strange behaivor of DataTrigger

JimmyPun.6276JimmyPun.6276 USMember ✭✭
edited September 3 in Xamarin.Forms

Hi,

I am going to make a Label to change its Text and TextColor in different condition
Here is the XAML code of the Label

          <Label     Grid.Column="1"
                           Text="No Preference"
               TextColor="Black"
                           HorizontalOptions="FillAndExpand" HorizontalTextAlignment="End">
                        <Label.Triggers>
                            <DataTrigger TargetType="Label" Binding="{Binding PropertyType}" Value="{x:Static enum:Types.House}">
                                <Setter Property="Text" Value="House"/>
                                <Setter Property="TextColor" Value="Red"/>
                            </DataTrigger>
                            <DataTrigger TargetType="Label" Binding="{Binding PropertyType}" Value="{x:Static enum:Types.Flat}">
                                <Setter Property="Text" Value="Flat"/>
                                <Setter Property="TextColor" Value="Blue"/>
                            </DataTrigger>
                        </Label.Triggers>
                    </Label>

PropertyType is a enum variable and the enum is

public enum Types{
    None = 0,
    House = 1,
    Flat = 2
}

So when I init the UI, the default PropertyType is Types.None (0), it is fine.
Then I do below changes and some unexpected results come out.
1. Change from "None" to "House", it also working fine that the UI show "House" in red.
2. Then I change from "House" to "Flat". It also working fine.
3. Change from "Flat" to "House". The UI shows "No Preference" in Red.

The DataTrigger only changes some parts but not all.
My device is Google Pixel 3 but I think the same issue may occur in iOS also.

Xamarin Form version 4.7.0.1239

Attached the sample app

Best Answer

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Sorry,I couldn't reproduce this question on my side after I tested your code.

    Since you have a Type: None = 0, why not add DataTrigger for it?

    After I added the DataTrigger for Type None, the result is as follows:

    The whole code is as follows:

        <StackLayout>
            <Label Padding="30,24,30,0"
                   Text="No Preference"
                   TextColor="Green"
                   FontSize="20">
                <Label.Triggers>
    
                    <DataTrigger TargetType="Label" Binding="{Binding Types}" Value="{x:Static local:Types.None}">
                        <Setter Property="Text" Value="None"/>
                        <Setter Property="TextColor" Value="Green"/>
                    </DataTrigger>
                    <DataTrigger TargetType="Label" Binding="{Binding Types}" Value="{x:Static local:Types.House}">
                        <Setter Property="Text" Value="House"/>
                        <Setter Property="TextColor" Value="Red"/>
                    </DataTrigger>
                    <DataTrigger TargetType="Label" Binding="{Binding Types}" Value="{x:Static local:Types.Flat}">
                        <Setter Property="Text" Value="Flat"/>
                        <Setter Property="TextColor" Value="Blue"/>
                    </DataTrigger>
                </Label.Triggers>
            </Label>
    
            <Button Text="Set to None" Command="{Binding SetTypesCommand}" CommandParameter="{x:Static local:Types.None}"/>
            <Button Text="Set to House" Command="{Binding SetTypesCommand}" CommandParameter="{x:Static local:Types.House}"/>
            <Button Text="Set to Flat" Command="{Binding SetTypesCommand}" CommandParameter="{x:Static local:Types.Flat}"/>
        </StackLayout>
    

    Hope it can help you.

  • JimmyPun.6276JimmyPun.6276 USMember ✭✭
    edited September 3

    @jezh said:
    Sorry,I couldn't reproduce this question on my side after I tested your code.

    Since you have a Type: None = 0, why not add DataTrigger for it?

    After I added the DataTrigger for Type None, the result is as follows:

    The whole code is as follows:

        <StackLayout>
            <Label Padding="30,24,30,0"
                   Text="No Preference"
                   TextColor="Green"
                   FontSize="20">
                <Label.Triggers>
    
                    <DataTrigger TargetType="Label" Binding="{Binding Types}" Value="{x:Static local:Types.None}">
                        <Setter Property="Text" Value="None"/>
                        <Setter Property="TextColor" Value="Green"/>
                    </DataTrigger>
                    <DataTrigger TargetType="Label" Binding="{Binding Types}" Value="{x:Static local:Types.House}">
                        <Setter Property="Text" Value="House"/>
                        <Setter Property="TextColor" Value="Red"/>
                    </DataTrigger>
                    <DataTrigger TargetType="Label" Binding="{Binding Types}" Value="{x:Static local:Types.Flat}">
                        <Setter Property="Text" Value="Flat"/>
                        <Setter Property="TextColor" Value="Blue"/>
                    </DataTrigger>
                </Label.Triggers>
            </Label>
    
            <Button Text="Set to None" Command="{Binding SetTypesCommand}" CommandParameter="{x:Static local:Types.None}"/>
            <Button Text="Set to House" Command="{Binding SetTypesCommand}" CommandParameter="{x:Static local:Types.House}"/>
            <Button Text="Set to Flat" Command="{Binding SetTypesCommand}" CommandParameter="{x:Static local:Types.Flat}"/>
        </StackLayout>
    

    Hope it can help you.

    To be specified on the reproduce steps,
    1. Tap "Set To House"
    2. Tap "Set To Flat"
    3. Tap "Set To House"
    4. Tap "Set to None" <===This will occur the bug.

    After I added DataTrigger for None. The problem in the testing app looks fine but my real app is still not working

    The gif is showing the behavior of the original code (not adding datatrigger for None)

  • jezhjezh Member, Xamarin Team Xamurai

    To be specified on the reproduce steps,
    1. Tap "Set To House"
    2. Tap "Set To Flat"
    3. Tap "Set To House"
    4. Tap "Set to None" <===This will occur the bug.

    Sorry, according to your steps, I still can't reproduce this problem. My test device is Google Pixel (Android 8.0).

    Have you tried test on other devices?

  • JimmyPun.6276JimmyPun.6276 USMember ✭✭
    edited September 3
    > @jezh said:
    > (Quote)
    > Sorry, according to your steps, I still can't reproduce this problem. My test device is Google Pixel (Android 8.0).
    >
    > Have you tried test on other devices?

    I tested on Google Pixel 3 with Android 10
    and google pixel 2 in simulator with Android 10.

    And the same is happening in iOS 13.7 simulator.
  • jezhjezh Member, Xamarin Team Xamurai

    I tested on google pixel2 in simulator with Android 10, the app also works well.

    You can recheck to see if your newly modified code has been redeployed to the device.

  • JimmyPun.6276JimmyPun.6276 USMember ✭✭
    edited September 7

    @jezh said:
    I tested on google pixel2 in simulator with Android 10, the app also works well.

    You can recheck to see if your newly modified code has been redeployed to the device.

    Same result for the sample app and my real app in both Android and iOS.
    I am using the latest version of the tools

    I think it is similar to this thread
    https://github.com/xamarin/Xamarin.Forms/issues/9495

    I also create github thread on
    https://github.com/xamarin/Xamarin.Forms/issues/12040

Sign In or Register to comment.