2013-03-01 78 views
0

我是WinRT/XAML開發新手。經過幾個小時的網絡研究和許多嘗試和錯誤嘗試後,我仍然無法理解如何使用VisualStateManager根據用戶對對象的輸入更改橢圓的填充顏色。以下代碼不起作用。下面是代碼今天因爲它位於:WinRT/XAML在鼠標上改變橢圓的填充顏色

<Ellipse Stroke="White" StrokeThickness="5" Width="90" Height="90"> 
     <VisualStateManager.VisualStateGroups> 
      <VisualStateGroup x:Name="CommonStates"> 
       <VisualState x:Name="Normal"/> 
       <VisualState x:Name="MouseOver"> 
        <Storyboard> 
         <ColorAnimation To="Red" Storyboard.TargetName="Ellipse" Storyboard.TargetProperty="Fill.Color"/> 
        </Storyboard> 
       </VisualState> 
       <VisualStateGroup.Transitions> 
        <VisualTransition To="Normal" GeneratedDuration="00:00:01"/> 
        <VisualTransition To="MouseOver" GeneratedDuration="00:00:01"/> 
       </VisualStateGroup.Transitions> 
      </VisualStateGroup> 
     </VisualStateManager.VisualStateGroups> 
    </Ellipse> 

UPDATE:

謝謝尼古拉斯W.在正確的方向輕推。我錯過了模板以及正確的目標屬性。下面的代碼如預期運行:

<Button> 
      <Button.Template> 
       <ControlTemplate> 
        <Grid> 
         <Ellipse x:Name="myEllipse" Stroke="White" StrokeThickness="5" Width="70" Height="70" Fill="Transparent"/> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="Pressed"> 
            <Storyboard> 
             <ColorAnimation Storyboard.TargetName="myEllipse" To="#FF0061D4" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Duration="0:0:0"/> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
        </Grid> 
       </ControlTemplate> 
      </Button.Template> 
     </Button> 
+0

我的WinRT技能是有限的;但是TargetName會拒絕一個不存在的元素。 Ellipse是對象名稱,而不是它的「名稱」,嘗試刪除TargetName,然後動畫將定位該元素本身。如果這不起作用,請給你Ellipse一個名字和參考。 – dowhilefor 2013-03-01 15:05:56

回答

1

有一些問題在這裏,首先沒有什麼導致視覺狀態管理狀態之間轉換,其次是參照「橢圓」目標沒有解決,第三,沒有定義要執行彩色動畫的畫筆。如果您要重新設定一個已經在使用視覺狀態的控件,第一部分將會爲您完成,否則您需要設置顯式狀態轉換(example)。要使參考起作用,您可以爲該元素指定一個名稱,並且不要將VisualStateGroups附加屬性嵌套在元素本身中(或者通過畫筆名稱的動畫as per the MSDN example)。最後一部分只涉及到最初設置畫筆,在該畫筆上您爲Color屬性設置了動畫。以模擬示例Button

<Button> 
     <Button.Template> 
      <ControlTemplate> 
       <Grid> 
        <Ellipse x:Name="Ellipse" Stroke="White" StrokeThickness="5" Width="90" Height="90" 
          Fill="Black"/> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"/> 
          <VisualState x:Name="MouseOver"> 
           <Storyboard> 
            <ColorAnimation Storyboard.TargetName="Ellipse" To="Red" Storyboard.TargetProperty="Fill.Color"/> 
           </Storyboard> 
          </VisualState> 
          <VisualStateGroup.Transitions> 
           <VisualTransition To="Normal" GeneratedDuration="00:00:01"/> 
           <VisualTransition To="MouseOver" GeneratedDuration="00:00:01"/> 
          </VisualStateGroup.Transitions> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
       </Grid> 
      </ControlTemplate> 
     </Button.Template> 
    </Button>