maui
maui copied to clipboard
DataTrigger.Value Does Not Respond to Data Binding
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
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.
I'm having the same issue. Anyone know of a workaround?
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.
So, does anyone know a definitive answer if binding can or cannot be used in the Value property of a DataTrigger?
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
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.
Reproduced issue using sample project on Windows, Android 11 and 13, and iOS 17 using latest Preview version of VS and .NET8.
Is there any movement on getting Data triggers to work properly? I'm having some issues with data triggers as well
Still an issue. Can we get this fixed? This seems like a no-brainer. Documentation needs to be adjusted, otherwise.
Any updates on getting this fixed?