110,533
社区成员
发帖
与我相关
我的任务
分享
<Button FontSize="16" FontWeight="Bold" x:Name="btnBackground" Background="AliceBlue">Click Me
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
然后后台代码中定义一个bool变量,在button的click事件中写代码改变背景色:
bool flag = true;
void btnBackground_Click(object sender, RoutedEventArgs e)
{
if (flag)
{
this.btnBackground.Background = new SolidColorBrush(Colors.Blue);
}
else
{
this.btnBackground.Background = new SolidColorBrush(Colors.AliceBlue);
}
flag = !flag;
}