WPF 自定义图片按钮

迷南。 2021-11-05 11:26 629阅读 0赞

WPF 自定义图片按钮

原文: WPF 自定义图片按钮

此文档仅仅是一个BaseCode,已做后续查阅

XAML代码:

ContractedBlock.gif ExpandedBlockStart.gif

  1. <Button x:Class="IM.UI.UC.IM_ImageButton"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Name="loc">
  5. <Image Name="innerImage" Stretch="None" Source="{Binding Path=ImageSourceEx,ElementName=loc}" />
  6. </Button>

CS代码:

ContractedBlock.gif ExpandedBlockStart.gif

  1. public IM_ImageButton()
  2. {
  3. InitializeComponent();
  4. FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(IM_ImageButton), new FrameworkPropertyMetadata(typeof(Button)));
  5. this.IsEnabledChanged += new DependencyPropertyChangedEventHandler(ImageButton_IsEnabledChanged);
  6. }
  7. public ImageSource GrayImageSourceEx
  8. {
  9. get { return (ImageSource)GetValue(GrayImageSourceExProperty); }
  10. set { SetValue(GrayImageSourceExProperty, value); }
  11. }
  12. public static readonly DependencyProperty GrayImageSourceExProperty =
  13. DependencyProperty.Register("GrayImageSourceEx", typeof(ImageSource), typeof(IM_ImageButton), new UIPropertyMetadata(null));
  14. public ImageSource ImageSourceEx
  15. {
  16. get { return (ImageSource)GetValue(ImageSourceExProperty); }
  17. set { SetValue(ImageSourceExProperty, value); }
  18. }
  19. public static readonly DependencyProperty ImageSourceExProperty =
  20. DependencyProperty.Register("ImageSourceEx", typeof(ImageSource), typeof(IM_ImageButton), new UIPropertyMetadata(null));
  21. //当前按钮显示状态被禁用或启用
  22. protected void ImageButton_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
  23. {
  24. if (this.IsEnabled && (this.ImageSourceEx != null))
  25. {
  26. this.innerImage.Source = this.ImageSourceEx;
  27. }
  28. else if (!(this.IsEnabled || (this.GrayImageSourceEx == null)))
  29. {
  30. this.innerImage.Source = this.GrayImageSourceEx;
  31. }
  32. }

posted on 2019-04-24 18:24 NET未来之路 阅读( …) 评论( …) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/10764056.html

发表评论

表情:
评论列表 (有 0 条评论,629人围观)

还没有评论,来说两句吧...

相关阅读

    相关 WPF-按钮美化

      我们不多哔哔,先放图: ![1699486-20190527161453388-1302609105.png][] 美化按钮背景:   当我们用系统默认的按钮