博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF后台自定义文字带背景的选择状态按钮
阅读量:4678 次
发布时间:2019-06-09

本文共 3420 字,大约阅读时间需要 11 分钟。

如果要转载请注明来出处(),谢谢!

 

描述:可以对按钮的选择或未选择时的文字颜色以及背景图片进行变换。

 

效果如下:

 

 

 

首先是自定义按钮类:

ExpandedBlockStart.gif
View Code
 
public 
class TextImageButton:Button
    {
        
public TextImageButton()
        {
            
this.IsSelectedChanged += 
new IsSelectedChangedEventHandler(TextImageButton_IsSelectedChanged);
        }
        
private 
void TextImageButton_IsSelectedChanged(
object sender, DependencyPropertyChangedEventArgs e)
        {
            
if (
this.IsSelected == 
true)
            {
                
this.TextColor = SelectedTextColor;
                
this.Background = SelectedBackground;
            }
            
else
            {
                
this.TextColor = UnSelectedTextColor;
                
this.Background = UnSelectedBackground;
            }
        }
        
public 
delegate 
void IsSelectedChangedEventHandler(
object sender, DependencyPropertyChangedEventArgs e);
        
public 
event IsSelectedChangedEventHandler IsSelectedChanged;
        
private 
bool isSelected = 
false;
        
public 
bool IsSelected
        {
            
get{
return isSelected;}
            
set
            {
                
if (isSelected != value)
                {
                    isSelected = value;
                    IsSelectedChanged(
this,
new DependencyPropertyChangedEventArgs());
                }
            }
        }
        
public 
new 
static DependencyProperty BackgroundProperty = DependencyProperty.RegisterAttached
        (
            
"
Background
",
            
typeof(ImageSource),
            
typeof(Button)
        );
        
public 
new ImageSource Background
        {
            
get
return (ImageSource)GetValue(BackgroundProperty);}
            
set { SetValue(BackgroundProperty, value); }
        }
        
public 
static DependencyProperty TextColorProperty = DependencyProperty.RegisterAttached
        (
            
"
TextColor
",
            
typeof(SolidColorBrush),
            
typeof(Button)
        );
        
public SolidColorBrush TextColor
        {
            
get { 
return (SolidColorBrush)GetValue(TextColorProperty); }
            
set { SetValue(TextColorProperty, value); }
        }
        
public 
static DependencyProperty TextProperty = DependencyProperty.RegisterAttached
        (
            
"
Text
",
            
typeof(
string),
            
typeof(Button)
        );
        
public 
string Text
        {
            
get { 
return (
string)GetValue(TextProperty); }
            
set { SetValue(TextProperty, value); }
        }
        
public ImageSource SelectedBackground { 
get
set; }
        
public ImageSource UnSelectedBackground { 
get
set; }
        
public SolidColorBrush SelectedTextColor { 
get
set; }
        
public SolidColorBrush UnSelectedTextColor { 
get
set; }      
    }

 

其次是创建按钮模板:

ExpandedBlockStart.gif
View Code
    
<
ControlTemplate 
TargetType
="Button"
 x:Key
="SelectButton"
>
        
<
Grid 
Cursor
="Hand"
>
            
<
Image 
Source
="
{Binding Path=Background,RelativeSource={RelativeSource TemplatedParent}}
"
></
Image
>
            
<
TextBlock 
FontFamily
="微软雅黑"
 FontSize
="24"
 Width
="220"
 HorizontalAlignment
="Center"
 VerticalAlignment
="Center"
 TextAlignment
="Center"
 Text
="
{Binding Path=Text,RelativeSource={RelativeSource TemplatedParent}}
"
 Foreground
="
{Binding Path=TextColor,RelativeSource={RelativeSource TemplatedParent}}
"
 TextWrapping
="Wrap"
></
TextBlock
>
        
</
Grid
>
    
</
ControlTemplate
>

 

最后就可以在后台中直接动态创建:

ExpandedBlockStart.gif
View Code
            TextImageButton tbi = 
new TextImageButton()
            {
                Width = 
280,
                Height = 
100,
                Template = 
this.Resources[
"
SelectButton
"
as ControlTemplate,
                Background = 
new BitmapImage(
new Uri(
@"
/项目名称;component/Images/UnSelectButton.png
", UriKind.Relative)),
                SelectedBackground = 
new BitmapImage(
new Uri(
@"
/项目名称;component/Images/SelectButton.png
", UriKind.Relative)),
                UnSelectedBackground = 
new BitmapImage(
new Uri(
@"
/项目名称;component/Images/UnSelectButton.png
", UriKind.Relative)),
                TextColor = 
new SolidColorBrush(Color.FromRgb(
51
51
51)),
                SelectedTextColor = 
new SolidColorBrush(Color.FromRgb(
255
255
255)),
                UnSelectedTextColor = 
new SolidColorBrush(Color.FromRgb(
51
51
51)),
                Name = 
"
按钮名称
",
                Text = 
"
按钮内容
"
            };

 变换按钮的状态通过 tbi.IsSelected = true 或 tbi.IsSelected = false 即可

转载于:https://www.cnblogs.com/therock/archive/2012/05/14/2499527.html

你可能感兴趣的文章
如何避免在简单业务逻辑上面的细节上面出错
查看>>
大型网站高并发的架构演变图-摘自网络
查看>>
8丶运行及总结
查看>>
Design Pattern --- Strategy
查看>>
mui列表跳转到详情页优化方案
查看>>
一些简单有用的方法合集
查看>>
Neutron 架构 - 每天5分钟玩转 OpenStack(67)
查看>>
详解JS设计模式
查看>>
CPSR寄存器
查看>>
Java基础50题test7—处理字符串
查看>>
保险行业电话外呼型呼叫中心方案
查看>>
自建型呼叫中心
查看>>
input file 文件上传,js控制上传文件的大小和格式
查看>>
Day 6 函数与模块
查看>>
WebApi请求原理
查看>>
[Node.js] node-persist: localStorage on the server
查看>>
jquery.event 研究学习之bind篇
查看>>
LOJ #108. 多项式乘法
查看>>
libusb开发指南
查看>>
SAS基础 -- 逻辑库不存在问题解决
查看>>