以下样式会导致异常"'ContentElement'名称在'System.Windows.Controls.Grid'的名称范围内找不到"当应用它的文本框进入禁用状态时。 我在犯规现场添加了评论。 知道我如何纠正这一点而不仅仅是注释掉吗?
<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>
我认为您根本不需要指定目标,将其删除,事情应该开始工作
Storyboard.TargetName="ContentElement"
它看起来像一个简单的复制/粘贴错误。但以防万一,您不必在自己的样式触发器上指定目标(仅当它在模板触发器中并且您正在操作模板中的各种元素,或者动画在您的样式之外声明,并且您必须引用一个元素......
我假设提供ColorAnimationUsingKeyFrames的目的是在框从启用变为禁用时执行一些颜色动画。
但是,控制背景网格不透明度的DoubleAnimationUsingKeyFrames已经完成了这项工作,因此我认为您不需要包含ColorAnimationUsingKeyFrames。
正如其他人所说,您的直接问题正是错误消息所说的:控件模板中没有一个名为"ContentElement"的元素。我认为,您最好的选择是删除整个ColorAnimationUsingKeyFrames。
是文本框的名称"ContentElement",如果不是,它应该是。
如果您查看文档中的示例...
http://msdn.microsoft.com/en-us/library/system.windows.media.animation.storyboard.targetname.aspx
。然后,您将看到必须为序列图像板.目标名称指定 XAML 中有效项的名称。