在ControlTemplate的触发器中,如何访问在ControlTemplate主体中定义的元素?

| 我有以下(简化的)ControlTemplate:
<ControlTemplate x:Key=\"ImageButtonTemplate\" TargetType=\"{x:Type controls:ImageButton}\">
    <Grid x:Name=\"Grid\">
      <Border [StuffRemoved]/>
      <StackPanel [StuffRemoved]>
        <Image [StuffRemoved] />
        <ContentPresenter [StuffRemoved] />
      </StackPanel>      

        <Grid.RenderTransform>
            <ScaleTransform x:Name=\"ImgBtnScale\"/>
        </Grid.RenderTransform>
    </Grid>

    <ControlTemplate.Triggers>

        <!-- problem here! -->
        <Trigger Property=\"IsPressed\" Value=\"True\">
            <Trigger.Setters>
                <Setter  TargetName=\"ImgBtnScale\" Property=\"ScaleX\" Value=\"0.9\"/>
                <Setter TargetName=\"ImgBtnScale\" Property=\"ScaleY\" Value=\"0.9\"/>
            </Trigger.Setters>
        </Trigger>      


        <!-- rest of the stuff works okay-->

        <Trigger Property=\"IsMouseOver\" Value=\"True\">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName=\"ImgBtnScale\" Storyboard.TargetProperty=\"ScaleX\"
                            To=\"1.2\" Duration=\"0:0:0.1\"/>
                        <DoubleAnimation Storyboard.TargetName=\"ImgBtnScale\" Storyboard.TargetProperty=\"ScaleY\"
                            To=\"1.2\" Duration=\"0:0:0.1\"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>

            <Trigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName=\"ImgBtnScale\" Storyboard.TargetProperty=\"ScaleX\"
                            To=\"1\" Duration=\"0:0:0.1\"/>
                        <DoubleAnimation Storyboard.TargetName=\"ImgBtnScale\" Storyboard.TargetProperty=\"ScaleY\"
                            To=\"1\" Duration=\"0:0:0.1\"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.ExitActions>      
        </Trigger>

    </ControlTemplate.Triggers>
  </ControlTemplate>
它不编译,说:
Cannot find the target \'ImgBtnScale\'. (The  target must appear before any Setters, Triggers or Conditions that use it.)
那么我如何访问
ImgBtnScale
已邀请:
您无法按名称访问它,因为
ScaleTransform
不是视觉树中的元素,它只是元素属性的值(
Grid
)。 相反,您可以做的是通过定义转换的父元素访问转换的属性。例如:
<DoubleAnimation Storyboard.TargetName=\"Grid\" Storyboard.TargetProperty=\"RenderTransform.(ScaleTransform.ScaleX)\" .../>
我认为问题是ScaleTransform不是视觉元素。因此无法在VisualTree中找到它。您应该尝试在网格上设置触发器并更改整个ScaleTransform。

要回复问题请先登录注册