Search code examples
triggersreleasemaui

MAUI DataTrigger does not work in Release mode?


This is a mystery to me. I have a button like so:

<Button
    Text="{x:Static res:AppRes.SaveAsDraft}"
    BorderColor="#2b3c3c"
    BackgroundColor="#001933"
    CornerRadius="10"
    TextColor="White"
    HorizontalOptions="CenterAndExpand"
    WidthRequest="120"
    HeightRequest="40"
    Margin="0,0,10,10"
    IsVisible="True"
    VerticalOptions="Center"
    Command="{Binding SaveAsDraftCommand}">
    <Button.Triggers>
        <DataTrigger TargetType="Button" Binding="{Binding ChangesWereMade}" Value="True">
            <Setter Property="IsEnabled" Value="True" />
            <Setter Property="BackgroundColor" Value="#001933" />
        </DataTrigger>
        <DataTrigger TargetType="Button" Binding="{Binding ChangesWereMade}" Value="False">
            <Setter Property="IsEnabled" Value="False" />
            <Setter Property="BackgroundColor" Value="Gray" />
        </DataTrigger>                        
    </Button.Triggers>
</Button>

In Design it works like a charm. When I set the value [ObservableProperty] changesWereMade to true in my VM, the button gets enabled, and it shows as such. When that property is false, the button gets disabled.

In Release mode, however, not so much... When I set changesWereMade to true, the button gets enabled, it works if you know to click on it, it saves the data, but it is still greyed out - you have to know that it just looks disabled, but it is actually enabled. When that property is false, the button gets disabled for real. The appearance is still greyed out, of course, but now clicking on it does not work. Now it's correct.

So.... Why does my button work properly in both design and release, but in release it does not change its appearance to really signal "Hey, I'm now enabled" ?

Long story short:

in both design and release, the property "IsEnabled" works as expected. The property "BackgroundColor", however, only works in design.

Why ?

Thank you Alex.


Solution

  • Yes, I can reproduce this issue. It's really weird. You may raise an issue on Github if you want.

    For a workaround, you could use Background property instead of BackgroundColor property.

    <Button.Triggers>
        <DataTrigger TargetType="Button" Binding="{Binding ChangesWereMade}" Value="True">
            <Setter Property="IsEnabled" Value="True" />
            <Setter Property="Background" Value="#001933" />
        </DataTrigger>
        <DataTrigger TargetType="Button" Binding="{Binding ChangesWereMade}" Value="False">
            <Setter Property="IsEnabled" Value="False" />
            <Setter Property="Background" Value="Gray" />
        </DataTrigger>                        
    </Button.Triggers>
    

    Don't worry. There is no much differences between Background and BackgroundColor, you could refer to .net MAUI Background vs BackgroundColor - what is the difference?.

    Hope it works for you.