简体   繁体   English

从孩子访问父母的名字范围

[英]Accessing parent's namescope from a child

In XAML, I have code like this: 在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>

This code fails with a message saying that "TextColor" wasn't found in the border's namescope. 此代码失败,并显示一条消息,指出在边框的名称范围中未找到“ TextColor”。 How do I access the namescope where the TextColor is defined then? 如何访问定义了TextColor的名称范围? The ColorAniamtion is supposed to access the setter with the foreground property and change the color. ColorAniamtion应该使用前台属性访问设置器并更改颜色。

Animate the Foreground property of the button instead of animating the Setter . 对按钮的Foreground属性进行动画处理,而不是对Setter进行动画处理。 Since the Foreground property has the type Brush as opposed to Color , I am using a object animation instead of a color animation in the sample code below: 由于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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM