maui icon indicating copy to clipboard operation
maui copied to clipboard

DataTrigger.Value Does Not Respond to Data Binding

Open Quaybe opened this issue 2 years ago • 9 comments

Description

Simple repro. With or without MVVM and Community Toolkit helpers, the issue occurs for me on Android and Windows. Maybe DataTrigger.Value isn't meant to use data bindings, but that is not in the documentation if so, and it seems logically it should.

When using a DataTrigger, when DataBinding the DataTrigger.Value to a value in the ViewModel or code behind, the trigger does not work:

<VerticalStackLayout Grid.Row="1" Grid.Column="1" >
            <Label Text="{Binding Int1}" 
               FontSize="20"
               Padding="7" >
                <Label.Triggers>
                    <DataTrigger TargetType="Label"
                             Binding="{Binding Int1}"
                             Value="{Binding Int2}">
                        <Setter Property="BackgroundColor"
                            Value="DarkSeaGreen"/>
                    </DataTrigger>
                </Label.Triggers>
            </Label>
            <Label Text="{Binding Int2}"
               FontSize="20"
               Padding="7" >
            </Label>
        </VerticalStackLayout>

If DataTrigger.Value is explicitly set to a value such as "2", it works:

        <VerticalStackLayout Grid.Row="3" Grid.Column="1" >
            <Label Text="{Binding Int1}" 
               FontSize="20"
               Padding="7" >
                <Label.Triggers>
                    <DataTrigger TargetType="Label"
                             Binding="{Binding Int1}"
                             Value="2">
                        <Setter Property="BackgroundColor"
                            Value="DarkSeaGreen"/>
                    </DataTrigger>
                </Label.Triggers>
            </Label>
            <Label Text="{Binding Int2}"
               FontSize="20"
               Padding="7" >
            </Label>
        </VerticalStackLayout>

For the DataTrigger in this example, if Int1 = Int2, I expect the label background to turn DarkSeaGreen, regardless if DataTrigger.Value is explicitly typed, or generated via data binding.

Int1 and Int2 are initialized in the ViewModel (using Community Toolkits mentioned above):

        [ObservableProperty]
        int int1 = 0;
        [ObservableProperty]
        int int2 = 2;

Steps to Reproduce

Create DataTrigger where DataTrigger.Value is a databinding.

When DataTrigger.Binding = DataTrigger.Value, the trigger should trigger.

Link to public reproduction project repository

https://github.com/Quaybe/TriggerTestMaui

Version with bug

8.0 (Current)

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows, iOS

Affected platform versions

Windows 10.0.19041.0, Android 10+, iOS 17

Did you find any workaround?

No response

Relevant log output

No response

Quaybe avatar Dec 27 '22 06:12 Quaybe

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Jan 12 '23 19:01 ghost

I'm having the same issue. Anyone know of a workaround?

williambuchanan2 avatar Apr 24 '23 04:04 williambuchanan2

I'm having the same issue. Anyone know of a workaround?

As a workaround, I have had to add extra logic to where if a criteria is met, I set a "status" property on my object to a number, and then I set the values on the trigger accordingly. So if 1 then color = green, if 2 then color = red, for example.

<Grid.Triggers> <DataTrigger TargetType="Grid" Binding="{Binding PullStatus}" Value="1"> <Setter Property="BackgroundColor" Value="LawnGreen"/> </DataTrigger> <DataTrigger TargetType="Grid" Binding="{Binding PullStatus}" Value="2"> <Setter Property="BackgroundColor" Value="Red"/> </DataTrigger> </Grid.Triggers>

So I'm explicitly setting the triggers to numbers, and using backend logic to change a status property to those numbers, so that I can use the triggers without using data binding on the "Value" of the trigger. It's simple enough, but does defeat the time-saving and step-saving purpose of data binding.

Quaybe avatar May 23 '23 15:05 Quaybe

So, does anyone know a definitive answer if binding can or cannot be used in the Value property of a DataTrigger?

MichaelShapiro avatar Jul 06 '23 18:07 MichaelShapiro

Verified this issue with Visual Studio Enterprise 17.8.0 Preview 1.0(.NET8). Can repro on windows platform with sample project. https://github.com/Quaybe/TriggerTestMaui

Zhanglirong-Winnie avatar Aug 18 '23 05:08 Zhanglirong-Winnie

Verified this issue with Visual Studio Enterprise 17.8.0 Preview 1.0(.NET8). Can repro on windows platform with sample project. https://github.com/Quaybe/TriggerTestMaui

Awesome.

Quaybe avatar Aug 22 '23 20:08 Quaybe

Reproduced issue using sample project on Windows, Android 11 and 13, and iOS 17 using latest Preview version of VS and .NET8.

Quaybe avatar Dec 07 '23 18:12 Quaybe

Is there any movement on getting Data triggers to work properly? I'm having some issues with data triggers as well

ChizuNoYama avatar Apr 10 '25 17:04 ChizuNoYama

Still an issue. Can we get this fixed? This seems like a no-brainer. Documentation needs to be adjusted, otherwise.

Quaybe avatar Jun 16 '25 16:06 Quaybe

Any updates on getting this fixed?

pme442 avatar Oct 01 '25 18:10 pme442