Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anything similar to WPF-DataTrigger in WinUI UWP project?

Tags:

c#

wpf

uwp

winui-3

I want to change the style based on binding values, for this I used DataTriggers in WPF. Now I'm trying to achieve the same in WinUI project, but since there are no DataTriggers in WinUI I can't go further.

In further analysis, I found the package Microsoft.Xaml.Behaviors.WinUI.Managed to use behaviors in WinUI project but I can't install this in WinUI UWP project.

Note: Since VisualStateManager only includes common states I can't apply that here.

like image 689
Karthik Raja AKR Avatar asked Dec 04 '25 10:12

Karthik Raja AKR


1 Answers

I found the package Microsoft.Xaml.Behaviors.WinUI.Managed to use behaviors in WinUI project but I can't install this in WinUI UWP project.

The available xaml behaviors package is Microsoft.Xaml.Behaviors.Uwp.Managed, you could install it into your uwp project, and use DataTriggerBehavior to control you xaml base on the specific bindind data.

<Rectangle x:Name="DataTriggerRectangle">
    <Interactivity:Interaction.Behaviors>
        <Interactions:DataTriggerBehavior Binding="{Binding Value, ElementName=slider}" ComparisonCondition="GreaterThan" Value="50">
            <Interactions:ChangePropertyAction TargetObject="{Binding ElementName=DataTriggerRectangle}" PropertyName="Fill" Value="{StaticResource PaleYellowBrush}"/>
        </Interactions:DataTriggerBehavior>
        <Interactions:DataTriggerBehavior Binding="{Binding Value, ElementName=slider}" ComparisonCondition="LessThanOrEqual" Value="50">
            <Interactions:ChangePropertyAction TargetObject="{Binding ElementName=DataTriggerRectangle}" PropertyName="Fill" Value="{StaticResource RoyalBlueBrush}"/>
        </Interactions:DataTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</Rectangle>

Here is official document that you could refer, and here is official code sample.

like image 95
Nico Zhu - MSFT Avatar answered Dec 06 '25 22:12

Nico Zhu - MSFT