成语大全网 - 汉语词典 - wpf里如何动态改变一个按钮模板里边的静态资源

wpf里如何动态改变一个按钮模板里边的静态资源

可以找到模板下面的控件喔,当然也可以修改其属性。给个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");

}