- 可以找到模板下面的控件喔,当然也可以修改其属性。给个demo给你
- 前台:
- <Grid>
- <Button Name="btn" Width="80" Height="30">
- <Button.Resources>
- <SolidColorBrush x:Key="redbrush" Color="Red"/>
- <SolidColorBrush x:Key="greenbrush" Color="Green"/>
- </Button.Resources>
- <Button.Template>
- <ControlTemplate>
- <Rectangle Name="rct" Fill="{StaticResource redbrush}" />
- </ControlTemplate>
- </Button.Template>
- </Button>
- <Button Click="button1_Click" Content="Button" Height="23" HorizontalAlignment="Left" Margin="190,222,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
- </Grid>
- 后台:
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- Rectangle rec = (Rectangle)btn.Template.FindName("rct", btn);
- rec.SetResourceReference(Rectangle.FillProperty, "greenbrush");
- }
复制代码
上面是我从百度知道找的demo,下面是我自己在项目中实践的。
- <Style x:Key="playButtonTemplate" TargetType="{x:Type Button}" >
- <!--修改模板属性-->
- <Setter Property="Template">
- <Setter.Value>
- <!--控件模板-->
- <ControlTemplate TargetType="{x:Type Button}">
- <Grid>
- <!--按钮呈圆形-->
- <Ellipse x:Name="outerCircle" Width="28" Height="28"/>
- <!--按钮内容-->
- <Border x:Name="border" >
- <Image x:Name="playImage" Height="25" Source="/BaPanVideo;component/Images/play.png" />
- </Border>
- </Grid>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
复制代码 前台模板
- <Button x:Name="btnplay" Cursor="Hand" Style="{StaticResource playButtonTemplate}" ToolTip="播放" Click="btnplay_Click" />
复制代码
动态修改:
- /// <summary>
- /// 修改控件模板
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void btntest_Click(object sender, RoutedEventArgs e)
- {
- Image img = (Image)btnplay.Template.FindName("playImage", btnplay);
- img.Source = new BitmapImage(new Uri("/BaPanVideo;component/Images/pause.png", UriKind.Relative));
- }
复制代码
|