读书人

WPF请问:怎样使按钮默认处于按下状态

发布时间: 2012-03-29 12:53:12 作者: rapoo

WPF请教:怎样使按钮默认处于按下状态
学习使用WPF改版软件时,有两个按钮a和b,a按下时b抬起,b按下时a抬起,总有一个按钮处于按下状态。请教高手怎样实现呢?初次发帖,有人理我没?小妹是新手没多少分啦,请谅解

[解决办法]

C# code
 //记录上次点击的按钮        private Button lastButton;        //改变列表中三个按钮被点击之后的背景图片        public void ChangeButtonBackground(Button btn)        {            //将当前点击按钮背景设置为点击下面的样式            LinearGradientBrush brushNow = new LinearGradientBrush();            brushNow.StartPoint = new Point(0, 0);            brushNow.EndPoint = new Point(0, 1);            brushNow.GradientStops.Add(new GradientStop(Color.FromArgb(0xFF, 0xEF, 0xEF, 0xEF), 0));            brushNow.GradientStops.Add(new GradientStop(Color.FromArgb(0xFF, 0xF8, 0xF7, 0xE9), 1));            DependencyObject dep = VisualTreeHelper.GetChild(btn, 0);            if ((dep != null) && (dep is Border))            {                (dep as Border).Background = brushNow;            }            //设置前一个按钮的背景为没点击状态样式            if (lastButton != null)            {                LinearGradientBrush brushLast = new LinearGradientBrush();                brushLast.StartPoint = new Point(0, 0);                brushLast.EndPoint = new Point(0, 1);                brushLast.GradientStops.Add(new GradientStop(Color.FromArgb(0xFF, 0xEF, 0xEF, 0xEF), 0));                brushLast.GradientStops.Add(new GradientStop(Color.FromArgb(0xFF, 0xC3, 0xCB, 0xCB), 1));                DependencyObject depLast = VisualTreeHelper.GetChild(lastButton, 0);                if ((depLast != null) && (depLast is Border))                {                    (depLast as Border).Background = brushLast;                }            }            this.lastButton = btn;        } 

读书人网 >CAD教程

热点推荐