Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggers aren't supported in Windows 8 XAML?

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.

like image 707
Darf Zon Avatar asked Nov 05 '22 02:11

Darf Zon


1 Answers

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);
like image 197
Amr Reda Avatar answered Nov 10 '22 05:11

Amr Reda