Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
715 views
in Technique[技术] by (71.8m points)

c# - 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...