2

当应用它的文本框进入禁用状态时,以下样式会导致异常“在 'System.Windows.Controls.Grid' 的名称范围内找不到'ContentElement' 名称”。我在犯规的地方添加了评论。知道如何纠正这个问题而不只是将其注释掉吗?

<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>
4

3 回答 3

1

我认为您根本不需要指定目标,将其删除,事情应该开始起作用

Storyboard.TargetName="ContentElement"

它看起来像一个简单的复制/粘贴错误。但以防万一,您不必在自己的样式触发器上指定目标(仅当它在模板触发器中并且您正在操作模板中的各种元素,或者动画在您的样式之外声明,并且您必须引用一个元素...

于 2012-04-13T15:25:44.823 回答
0

我假设提供 ColorAnimationUsingKeyFrames 的目的是在框从启用变为禁用时执行一些彩色动画。

但是,控制背景网格不透明度的 DoubleAnimationUsingKeyFrames 已经完成了这项工作,因此我认为您不需要包含 ColorAnimationUsingKeyFrames。

As others have said, your immediate problem is exactly what the error message says: the ControlTemplate does not have an element called "ContentElement" in it. Your best bet will be just to remove the whole of the ColorAnimationUsingKeyFrames I think.

于 2012-04-13T15:36:51.487 回答
-1

是你的TextBox的名称“ContentElement”,如果不是,应该是。

如果您查看文档中的示例...

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

...然后您将看到必须为 Storyboard.TargetName 指定 XAML 中有效项目的名称。

于 2012-04-13T15:15:45.507 回答