2012-04-13 79 views
2

當它應用於文本框進入禁用狀態時,以下樣式將導致在'System.Windows.Controls.Grid'的名稱範圍中找不到''ContentElement'名稱的異常。我在犯規現場添加了評論。任何想法如何我可以改正這一點,而不是隻是評論它?WPF文本框樣式拋出InvalidOperationException

<Style x:Key="ThemedTextBox" TargetType="{x:Type TextBox}"> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="Background" Value="#FFFFFFFF"/> 
     <Setter Property="Foreground" Value="White"/> 
     <Setter Property="Padding" Value="-1"/> 
     <Setter Property="BorderBrush"> 
      <Setter.Value> 
       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
        <GradientStop Color="#FFA3AEB9" Offset="0"/> 
        <GradientStop Color="#FF8399A9" Offset="0.375"/> 
        <GradientStop Color="#FF718597" Offset="0.375"/> 
        <GradientStop Color="#FF617584" Offset="1"/> 
       </LinearGradientBrush> 
      </Setter.Value> 
     </Setter> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type TextBox}"> 
        <Grid x:Name="RootElement"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualStateGroup.Transitions> 
            <VisualTransition GeneratedDuration="00:00:00.1" To="MouseOver"/> 
            <VisualTransition GeneratedDuration="00:00:00.1" To="ReadOnly"/> 
            <VisualTransition GeneratedDuration="00:00:00.1" To="Disabled"/> 
           </VisualStateGroup.Transitions> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="MouseOver"> 
            <Storyboard> 
             <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="MouseOverState" Storyboard.TargetProperty="(UIElement.Opacity)"> 
              <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/> 
             </DoubleAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <!--DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="DisabledState" Storyboard.TargetProperty="(UIElement.Opacity)"> 
              <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/> 
             </DoubleAnimationUsingKeyFrames> 
    <!--************** This is where the problem is **************--> 
           <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="ContentElement" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"> 
              <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF808080"/> 
             </ColorAnimationUsingKeyFrames--> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="ReadOnly"> 
            <Storyboard/> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="FocusStates"> 
           <VisualState x:Name="Focused"> 
            <Storyboard> 
             <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="MouseOverState" Storyboard.TargetProperty="(UIElement.Opacity)"> 
              <SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/> 
             </DoubleAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Unfocused"> 
            <Storyboard/> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 

         <Grid Margin="0,0,0,0" x:Name="NormalState"> 
          <Rectangle HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" RadiusX="0" RadiusY="0" Fill="{StaticResource GlossyBlack_TextBoxBackgroundGradient}"> 
          </Rectangle> 
          <Rectangle HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" RadiusX="0" RadiusY="0" Stroke="{StaticResource GlossyBlack_TextBoxStroke}"> 
          </Rectangle> 
          <Rectangle HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" RadiusX="0" RadiusY="0" Height="Auto" Fill="{StaticResource GlossyBlack_TextBoxHighlightGradient}"/> 

         </Grid> 
         <Grid Margin="0,0,0,0" x:Name="MouseOverState" Opacity="0"> 
          <Rectangle HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" RadiusX="0" RadiusY="0" Fill="{StaticResource GlossyBlack_TextBoxBackgroundGradient}"/> 
          <Rectangle HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" RadiusX="0" RadiusY="0" Stroke="{StaticResource GlossyBlack_TextBoxStroke}"/> 
          <Rectangle HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" RadiusX="0" RadiusY="0" Height="Auto" Fill="{StaticResource GlossyBlack_TextBoxMouseOverHighlight}"/> 

         </Grid> 
         <Grid Margin="0,0,0,0" x:Name="DisabledState" Opacity="0"> 
          <Rectangle HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" RadiusX="0" RadiusY="0" Fill="{StaticResource GlossyBlack_TextBoxDisabledBackground}"/> 
          <Rectangle HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" RadiusX="0" RadiusY="0" Stroke="{StaticResource GlossyBlack_TextBoxStroke}"/> 

         </Grid> 

         <ScrollViewer BorderThickness="0" IsTabStop="False" Padding="{TemplateBinding Padding}" Margin="4,4,4,4" x:Name="PART_ContentHost" BorderBrush="{x:Null}" Foreground="#FFFFFFFF"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
+1

如果您的風格沒有名稱爲ContentElement的元素,它將如何工作? – Nikolay 2012-04-13 14:29:48

回答

1

我不認爲你需要在所有的指定目標,刪除它,事情應該開始工作

Storyboard.TargetName="ContentElement" 

它看起來像一個簡單的複製/粘貼錯誤。但爲了以防萬一,您不必在自己的樣式觸發器上指定目標(僅當它在模板觸發器中,並且您正在操作模板中的各種元素,或者動畫是在樣式外聲明的,而且必須引用一個元素...

+0

只是測試它,它做到了.. – 2012-04-13 15:34:01

-1

是您的文本框「ContentElement」的名稱,如果不是,它應該是。

如果你看一下例子文檔中......

http://msdn.microsoft.com/en-us/library/system.windows.media.animation.storyboard.targetname.aspx

...那麼你會看到Storyboard.TargetName必須在XAML給出一個有效的項目的名稱。

+0

提到的「ContentElement」並不是指整個TextBox的名稱,它應該引用在ControlTemplate中定義的一個元素,它在其中被提及。調用TextBox「ContentElement」將無濟於事,因爲模板無法在其自身之外看到。 – 2012-04-13 15:30:53

+0

呃鮑勃......你應該在注意否定之前注意。提問者說他​​正在將它應用到TextBox中。如果他把TextBox的名字放在那裏,它就會起作用。 Denis Moroov也是正確的,因爲您可以完全刪除該行。 – Rhyous 2012-04-13 15:40:10

+0

如果我正確理解你的答案,你的建議是用提問者定義的樣式聲明文本框。即。不幸的是,這不起作用,因爲Style資源中定義的ContentTemplate無法看到該TextBox。如果我誤解了你,請澄清你的意思。謝謝。 – 2012-04-13 15:47:19

0

我假設提供ColorAnimationUsingKeyFrames的意圖是做一些色彩動畫時被啓用被禁用箱變。

然而,你DoubleAnimationUsingKeyFrames,它控制的不透明度背景網格已經非常多,所以我不認爲你需要包括ColorAnimationUsingKeyFrames。

作爲其他rs說過,你的直接問題正是錯誤信息所說的:ControlTemplate中沒有一個名爲「ContentElement」的元素。你最好的選擇就是去除我想的整個ColorAnimationUsingKeyFrames。

相關問題