silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了silverlight之How To:访问控件模板里的控件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

假设button控件应用了如下控件模板: <ControlTemplate x:Key="StartActivity" TargetType="button">             <Grid Width="Auto" Height="62" Margin="0,0,0,0">                 <TextBlock Height="0" Margin="0,0,0,0" Ver

假设button控件应用了如下控件模板:

  1. <ControlTemplate @H_696_29@x:Key="StartActivity" @H_696_29@TargetType="Button">
  2.             <Grid @H_696_29@Width="Auto" @H_696_29@Height="62" @H_696_29@margin="0,0">
  3.                 <TextBlock @H_696_29@Height="0" @H_696_29@margin="0,0" @H_696_29@VerticalAlignment="Bottom" @H_696_29@Text="" @H_696_29@textwrapping="Wrap" @H_696_29@x:Name="tbLabel" @H_696_29@RenderTransformOrigin="0.5,0.5" @H_696_29@HorizontalAlignment="Center" @H_696_29@Foreground="#FF0507FA">
  4.                 </TextBlock>
  5.             </Grid>
  6.         </ControlTemplate>

 

那么如果想在代码里访问模板里名为tbLabel的TextBlock控件,该怎么写代码呢?

控件基类Control有个叫GetTemplateChild的方法,但是该方法是Protected型的,所以很显然,我们必须继承基类并且重载OnApplyTemplate调用方法,如下:

  1. public class ActivityControl : Button
  2.     {
  3.         public override void OnApplyTemplate()
  4.         {
  5.             base.onApplyTemplate();
  6.          
  7.             //get the textblock control from template
  8.             textBlock label = GetTemplateChild("tbLabel"as textBlock;
  9.         }
  10.     }

大佬总结

以上是大佬教程为你收集整理的silverlight之How To:访问控件模板里的控件全部内容,希望文章能够帮你解决silverlight之How To:访问控件模板里的控件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。