WPF如何实现点击Button后背景色变成红色,再次点击变回原来颜色?

long龙儿er 2013-07-13 04:29:27
WPF如何实现点击Button后背景色变成红色,再次点击变回原来颜色?
...全文
1939 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
long龙儿er 2013-07-15
  • 打赏
  • 举报
回复
引用 6 楼 MicrosoftCenterOfHN 的回复:
你可以尝试把Template定义成一个application的resource, 设置targettype为button.
瞎弄的好像行了,O(∩_∩)O谢谢啊
  • 打赏
  • 举报
回复
你可以尝试把Template定义成一个application的resource, 设置targettype为button.
long龙儿er 2013-07-14
  • 打赏
  • 举报
回复
引用 6 楼 MicrosoftCenterOfHN 的回复:
你可以尝试把Template定义成一个application的resource, 设置targettype为button.
不懂啊
long龙儿er 2013-07-13
  • 打赏
  • 举报
回复
引用 4 楼 u010439291 的回复:
[quote=引用 3 楼 MicrosoftCenterOfHN 的回复:] 如果你要变回原来颜色的话,就先把原来的颜色保存一份。 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; }
非常感谢[/quote]
引用 2 楼 MicrosoftCenterOfHN 的回复:
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;
        }
若有多个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(); } 这想法行得通吗?
long龙儿er 2013-07-13
  • 打赏
  • 举报
回复
引用 3 楼 MicrosoftCenterOfHN 的回复:
如果你要变回原来颜色的话,就先把原来的颜色保存一份。 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; }
非常感谢
  • 打赏
  • 举报
回复
如果你要变回原来颜色的话,就先把原来的颜色保存一份。 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代码:
<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;
        }
Regan-lin 2013-07-13
  • 打赏
  • 举报
回复
你可以写一个bool值放到你的button里面,操作的时候进行判断下

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧