如果要轉載請注明來出處(http://www.cnblogs.com/therock),謝謝!
描述:可以對按鈕的選擇或未選擇時的文字顏色以及背景圖片進行變換。
效果如下:
首先是自定義按鈕類:
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; }
}
{
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; }
}
其次是創建按鈕模板:
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 >
< 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 >
最后就可以在后台中直接動態創建:
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 = " 按鈕內容 "
};
{
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 即可
