架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 22861|回复: 0

[WPF] WPF后台动态修改控件模板的属性值

[复制链接]
发表于 2016-3-16 18:00:21 | 显示全部楼层 |阅读模式
  1. 可以找到模板下面的控件喔,当然也可以修改其属性。给个demo给你
  2. 前台:
  3. <Grid>
  4.         <Button Name="btn" Width="80" Height="30">
  5.             <Button.Resources>
  6.                 <SolidColorBrush x:Key="redbrush" Color="Red"/>
  7.                 <SolidColorBrush x:Key="greenbrush" Color="Green"/>
  8.             </Button.Resources>
  9.             <Button.Template>
  10.                 <ControlTemplate>
  11.                     <Rectangle Name="rct" Fill="{StaticResource redbrush}" />
  12.                 </ControlTemplate>
  13.             </Button.Template>
  14.         </Button>
  15.         <Button Click="button1_Click" Content="Button" Height="23" HorizontalAlignment="Left" Margin="190,222,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
  16.     </Grid>
  17. 后台:
  18.   private void button1_Click(object sender, RoutedEventArgs e)
  19.         {
  20.             Rectangle rec = (Rectangle)btn.Template.FindName("rct", btn);

  21.             rec.SetResourceReference(Rectangle.FillProperty, "greenbrush");
  22.         }
复制代码



上面是我从百度知道找的demo,下面是我自己在项目中实践的。

  1. <Style x:Key="playButtonTemplate" TargetType="{x:Type Button}" >
  2.             <!--修改模板属性-->
  3.             <Setter Property="Template">
  4.                 <Setter.Value>
  5.                     <!--控件模板-->
  6.                     <ControlTemplate TargetType="{x:Type Button}">
  7.                         <Grid>
  8.                             <!--按钮呈圆形-->
  9.                             <Ellipse x:Name="outerCircle"   Width="28" Height="28"/>
  10.                             <!--按钮内容-->
  11.                             <Border x:Name="border"  >
  12.                                 <Image  x:Name="playImage" Height="25" Source="/BaPanVideo;component/Images/play.png" />
  13.                             </Border>
  14.                         </Grid>
  15.                     </ControlTemplate>
  16.                 </Setter.Value>
  17.             </Setter>
  18.         </Style>
复制代码
前台模板

  1. <Button  x:Name="btnplay" Cursor="Hand" Style="{StaticResource playButtonTemplate}" ToolTip="播放" Click="btnplay_Click" />
复制代码


动态修改:

  1. /// <summary>
  2.         /// 修改控件模板
  3.         /// </summary>
  4.         /// <param name="sender"></param>
  5.         /// <param name="e"></param>
  6.         private void btntest_Click(object sender, RoutedEventArgs e)
  7.         {
  8.             Image img = (Image)btnplay.Template.FindName("playImage", btnplay);
  9.             img.Source = new BitmapImage(new Uri("/BaPanVideo;component/Images/pause.png", UriKind.Relative));
  10.         }
复制代码






上一篇:wpf slider进度条的样式模板,带有进度颜色显示
下一篇:WPF下TextBox允许多行文本的设置
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-3-17 20:09

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表