5

Okay, if DataTriggers don't work anymore in Silverlight and Windows 8, could anyone tell me how to replace this feature?

For example;

In a ListView or GridView, if an item has a value x,

if x == "True"
 StackPanel style= "MakeBackgroundGreen"
else
 StackPanel style="MakeBackgroundRed"

Is there a way to create something like this in Windows 8 metro style app using XAML and C# (preferred C# but any language will do).

I've heard some people mention use VSM (Visual State Manager), how can I do this?

Thanks a lot in advance.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Darf Zon
  • 6,268
  • 20
  • 90
  • 149
  • Take a look also at: http://stackoverflow.com/questions/7439532/datatrigger-in-winrt – Amittai Shapira Apr 08 '12 at 07:00
  • 1
    Sorry? NOTHING has changed in XAML for WIndows 8. Are you talking of WinRT? DataTriggers work totally fine on my wpf application when running in windows 8. – TomTom May 21 '12 at 07:11

1 Answers1

2

You'll have to use Visual State Manager like this :

   <VisualStateManager.VisualStateGroups>

        <!-- Visual states reflect the application's view state -->
        <VisualStateGroup>
            <VisualState x:Name="FullScreenLandscape"/>
            <VisualState x:Name="Filled"/>

            <!-- The back button respects the narrower 100-pixel margin convention for portrait -->
            <VisualState x:Name="FullScreenPortrait">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>

            <!-- The back button and title have different styles when snapped -->
            <VisualState x:Name="Snapped">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                    </ObjectAnimationUsingKeyFrames>

                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

after that you can change state programmatically like this:

        VisualStateManager.GoToState(this, "stateName", true);
Amr Reda
  • 632
  • 7
  • 18