Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any better way to give conditional styling in uwp?

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

like image 302
Divakar Avatar asked Mar 22 '16 10:03

Divakar


1 Answers

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

like image 95
Eldho Avatar answered Oct 14 '22 11:10

Eldho