7

I have tried to give conditional styling using converter in style.setter as below,

    <Style TargetType="DataGrid">
        <Setter Property="Background" Value="{Binding Converter={StaticResource cc}}" />
    </Style>

and came to know that there is no support provided for using converter in UWP. So please anyone suggest me better way to provide conditional styling in UWP using converter in style.setter

1 Answer 1

5

No, we don't have Trigger support in UWP.

To keep the as much as light the triggers from UWP and Windows phone 8 are removed by msft. We could achieve those using Interactivity core. Blend(IDE) has great support to create trigger in these technologies.

Blend allows to define a behavior for the application Here.
We could define

  1. DataTrigger Use the DataTrigger trigger to invoke an action based on the value of a data-bound property
  2. EventTrigger Use the EventTrigger trigger to invoke an action based on an event such as a mouse click, a page loading, or another interaction.
  3. KeyTrigger Use the KeyTrigger trigger to invoke an action when a combination of keys is pressed on the keyboard.

Note:- This are trigger available for windows phone,make sure UWP have this trigger in the blend SDK

A workaround is to use DataTriggerBehavior with a ChangePropertyAction to accomplish this.

xmlns:ec="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" x:Class="XXX_XXXX"

This works for me

<DataGrid x:Name="MyGrid"
                   Stretch="None"
                   HorizontalAlignment="Stretch" 
                   VerticalAlignment="Top">
  <interactivity:Interaction.Triggers>
   <ec:DataTrigger Binding="{Binding IsBackgroundBlue}" Value="True">
       <ec:ChangePropertyAction TargetObject="{Binding ElementName=MyGrid}" PropertyName="Background" Value="Blue" />
     </ec:DataTrigger>
    <!--  You could add your conditions here />  -->
  </interactivity:Interaction.Triggers>
  </DataGrid>

Pls mind this may not be correct syntax i dont have IDE now

Similar answer in https://stackoverflow.com/a/31933556/1876572

Msdn reference of triggers using visual state manager

6
  • It is ok. But could you explain how we can apply styles based on condition using above code. i.e.,using triggers
    – Divakar
    Mar 22, 2016 at 11:56
  • Yeah. It would be helpful for me
    – Divakar
    Mar 22, 2016 at 12:08
  • I would try i dont have UWP in my visual studio, may have incorrect syntax
    – Eldho
    Mar 22, 2016 at 12:16
  • Ok no problem. Could you explain the above code you have given?
    – Divakar
    Mar 22, 2016 at 12:20
  • This puts the in the control itself. Is there anyway to implement this via a style? Jan 30, 2017 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.