关闭

WPF 模板中的控件修改

1312人阅读 评论(3) 收藏 举报

 模板:

<ControlTemplate x:Key="HomeBtnNewTemplate" TargetType="{x:Type Button}">
        <Grid Width="{TemplateBinding Width}"  Background="{TemplateBinding Background}"
      Height="{TemplateBinding Height}" ClipToBounds="True">
            <Rectangle x:Name="glassCube" HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch" Opacity="1"
        Fill="{StaticResource ShopBtnNewBrush}"  RenderTransformOrigin="0.5,0.5"
                        RadiusX="10" RadiusY="10">
                <Rectangle.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform />
                    </TransformGroup>
                </Rectangle.RenderTransform>
            </Rectangle>
        </Grid>

        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Rectangle.Opacity" Value="1"       TargetName="glassCube" />
            </Trigger>
            <Trigger Property="IsFocused" Value="true">
                <Setter Property="Rectangle.Opacity" Value="1"   TargetName="glassCube" />
            </Trigger>
            <EventTrigger RoutedEvent="Mouse.MouseEnter">
                <EventTrigger.Actions>
                    <BeginStoryboard Name="mouseEnterBeginStoryboard">
                        <Storyboard AutoReverse="True">
                            <DoubleAnimation Storyboard.TargetName="glassCube"
          Storyboard.TargetProperty=
          "(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
          By="0.03" Duration="0:0:0.2" />
                            <DoubleAnimation
        Storyboard.TargetName="glassCube"
          Storyboard.TargetProperty=
          "(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
          By="0.03" Duration="0:0:0.2" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
            <EventTrigger RoutedEvent="Mouse.MouseLeave">
                <EventTrigger.Actions>
                </EventTrigger.Actions>
            </EventTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

 

 

 

修改方法:

  ControlTemplate template = (ControlTemplate)this.TryFindResource("HomeBtnTemplate");           
            Rectangle rec = template.FindName("glassCube", btnTest) as Rectangle;
            Console.WriteLine(rec.Name);

同理可以拿到模板中的其他对象进行相应更改

 

0
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:75913次
    • 积分:1215
    • 等级:
    • 排名:千里之外
    • 原创:38篇
    • 转载:3篇
    • 译文:4篇
    • 评论:50条