Karthik Raja AKR
Karthik Raja AKR

Reputation: 93

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

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.

Upvotes: 2

Views: 4594

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32785

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.

Upvotes: 1

Related Questions