2014-05-15 214 views
0

訪問母公司的名稱範圍在XAML中,我有這樣的代碼:從孩子

<Style TargetType="Button"> 
    <Setter Property="Foreground" Value="#c10000" x:Name="TextColor"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Border x:Name="RootElement" CornerRadius="8"> 

        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 

          <VisualState x:Name="MouseOver"> 
           <Storyboard> 
            <ColorAnimation Storyboard.TargetName="TextColor" 
              Storyboard.TargetProperty="Foreground" To="#FF8D00" /> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

此代碼失敗,提示信息說,「TEXTCOLOR」在邊境的名稱範圍沒有被發現。如何訪問TextColor定義的名稱範圍呢? ColorAniamtion應該使用前景屬性訪問setter並更改顏色。

回答

0

動畫按鈕的Foreground屬性,而不是動畫Setter。由於Foreground酒店類型Brush而不是Color,我使用的是對象的動畫,而不是在下面的示例代碼的彩色動畫:

<Button.Style> 
    <Style TargetType="Button"> 
     <Setter Property="Foreground" Value="#c10000"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Border x:Name="RootElement" CornerRadius="8"> 
         <ContentPresenter/> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="MouseOver"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Button.Foreground)"> 
              <ObjectAnimationUsingKeyFrames.KeyFrames> 
               <DiscreteObjectKeyFrame KeyTime="0:0:0"> 
                <DiscreteObjectKeyFrame.Value> 
                 <SolidColorBrush Color="#FF8D00"/> 
                </DiscreteObjectKeyFrame.Value> 
               </DiscreteObjectKeyFrame> 
              </ObjectAnimationUsingKeyFrames.KeyFrames> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Button.Style> 
+0

這工作,但有可能增加持續時間也不知何故?所以顏色會在1秒內逐漸改變? – user3595338

+0

不,對於對象動畫,您不能執行漸變顏色更改。 –