2012-04-08 74 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);