WPF如何实现点击Button后背景色变成红色,再次点击变回原来颜色? WPF如何实现点击Button后背景色变成红色,再次点击变回原来颜色? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你可以写一个bool值放到你的button里面,操作的时候进行判断下 Button控件有点特殊,要想改变button的背景色,你要重写button的模板的。下面是一个示例,具体的你再自己写:xaml代码:<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; } 如果你要变回原来颜色的话,就先把原来的颜色保存一份。public MainWindow() { InitializeComponent(); this.btnBackground.Click += new RoutedEventHandler(btnBackground_Click); b = this.btnBackground.Background.Clone(); } bool flag = true; Brush b; void btnBackground_Click(object sender, RoutedEventArgs e) { if (flag) { this.btnBackground.Background = new SolidColorBrush(Colors.Red); } else { this.btnBackground.Background = b; } flag = !flag; } 若有多个Button都要实现这个效果,能不能在后台代码中派生一个Button的类,并且在构造函数中重写button的模板?这样就不用在xaml中写那么多代码了public class TestButton : Button { public bool IsSelect { get; set; } public TestButton() { this.IsSelect = false; this.Template.TargetType = typeof(Button); /******************/这里改怎么写啊??? } protected override void OnClick() { if (this.IsSelect == false) { this.Background = new SolidColorBrush(Colors.Red); this.IsSelect = true; } else { this.Background = new SolidColorBrush(Colors.White); this.IsSelect = false; } base.OnClick(); }这想法行得通吗? 你可以尝试把Template定义成一个application的resource, 设置targettype为button. C# 多线程 并行 WinForm Dependency Injection是什么东东?哪位能举个通俗易懂的小例子吗? 如何排序输出 什么原因会导致Debug和Release执行结果不一样? 谁会做winform的异步开发吗,我想后台在处理,前台显示进度条。 关于txt连接字符串问题! 关于checkbox的问题,希望各位高手帮帮忙! 批量动态生成控件的FindControl报错问题 大家给出出招吧,国际化问题 c#程序如何调用vc编译的动态连接库 Metro程序UI线程阻塞的问题? 用什么办法把str转换成MouseEventArgs或者EventArgs类型?本人菜鸟,求高手解决
下面是一个示例,具体的你再自己写:
xaml代码:
<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;
}
public MainWindow()
{
InitializeComponent();
this.btnBackground.Click += new RoutedEventHandler(btnBackground_Click); b = this.btnBackground.Background.Clone();
} bool flag = true;
Brush b;
void btnBackground_Click(object sender, RoutedEventArgs e)
{
if (flag)
{
this.btnBackground.Background = new SolidColorBrush(Colors.Red);
}
else
{
this.btnBackground.Background = b;
} flag = !flag;
}
若有多个Button都要实现这个效果,能不能在后台代码中派生一个Button的类,并且在构造函数中重写button的模板?这样就不用在xaml中写那么多代码了
public class TestButton : Button
{
public bool IsSelect { get; set; }
public TestButton()
{
this.IsSelect = false;
this.Template.TargetType = typeof(Button);
/******************/这里改怎么写啊???
} protected override void OnClick()
{
if (this.IsSelect == false)
{
this.Background = new SolidColorBrush(Colors.Red);
this.IsSelect = true;
}
else
{
this.Background = new SolidColorBrush(Colors.White);
this.IsSelect = false;
}
base.OnClick();
}
这想法行得通吗?