2012-04-08 68 views
5

好的,如果DataTriggers在Silverlight和Windows 8中不再工作,谁能告诉我如何替换这个功能?Windows 8 XAML不支持触发器?

例如;

在ListView或GridView控件,如果一个项目有一个值x,

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

有没有办法使用XAML和C#(首选C#,但任何语言来创建Windows 8中Metro风格应用程序是这样的会做)。

我听说有人提到使用VSM(Visual State Manager),我该怎么做?

非常感谢。

+0

看看也点击:http://计算器.com/questions/7439532/datatrigger-in-winrt – 2012-04-08 07:00:04

+1

对不起? XAML中没有什么改变了WIndows 8.你在谈论WinRT吗?在Windows 8中运行时,DataTriggers对我的wpf应用程序完全正常工作。 – TomTom 2012-05-21 07:11:17

回答

2

你将不得不使用视觉状态管理是这样的:

<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> 

后,您可以通过编程改变状态是这样的:

 VisualStateManager.GoToState(this, "stateName", true);