WPF 带 ICON 的 button 状态切换 (Enable/Disable)

本文介绍了在WPF中如何使带有ICON的button在禁用(Disable)时,图标变为灰色或半透明。提供了两种解决方案,一种是通过灰度处理使图片变灰,另一种是设置图片半透明。同时,给出了C#和XAML的实现代码,并展示了启用(Enable)和禁用(Disable)时的效果。
部署运行你感兴趣的模型镜像

问题的引出

在WPF中,如果一个button只有文字,没有图标,当disable的时候,文字就会变成灰色。如果这个button的内容中有图片,disable的时候,不会发生任何变化。那么,问题来了,如何使得图片在显示的时候也可以变成灰色,或者使得它变得不一样呢?

解决方案

两种方案,一种是图片变成灰色,另一种是变成半透明。对于方案一,当图片是彩色的时候,效果很好,但是如果图片本身是灰色的,则效果较差。第二种方案则相反。

图片变成灰色

如果想要使得图片变成灰色,那么需要用到类FormatConvertedBitmap。图片的显示方式,彩色或者灰色,是通过DestinationFormat 来控制的。参考WPF 将图片进行灰度处理
如果想要在C#代码中使用这个类,那么必须:

         FormatConvertedBitmap newFormatConvertedBitmap = new FormatConvertedBitmap();
         newFormatConvertedBitmap.BeginInit();
         newFormatConvertedBitmap.Source = oldFormatConvertedBitmap.Source;
         newFormatConvertedBitmap.DestinationFormat = PixelFormats.Gray8;
         newFormatConvertedBitmap.EndInit();

如果想要在xaml里面使用这个类:

         <!-- Normal button with icon gray & text is disabled -->
         <Button x:Name="IconGrayButton" Width="180" Height="32" IsEnabled="False" Margin="10">
            <StackPanel Orientation="Horizontal">
               <Image x:Name="IconGrayButtonImage">
                  <Image.Source>
                     <FormatConvertedBitmap Source="{StaticResource ButtonIcon}" DestinationFormat="Gray8">
                     </FormatConvertedBitmap>
                  </Image.Source>
               </Image>
               <Label Content="Gray Button"></Label>
            </StackPanel>
         </Button>

图片变成半透明

半透明则相对比较简单,通过属性Opacity即可控制。

         <!-- Normal button with icon & text is disabled -->
         <Button x:Name="NormalButton" Width="180" Height="32" IsEnabled="False" Margin="10">
            <StackPanel Orientation="Horizontal">
               <Image x:Name="NormalButtonImage" Source="{StaticResource ButtonIcon}" Opacity="0.5"></Image>
               <Label Content="Normal Button"></Label>
            </StackPanel>
         </Button>

完整代码

实现三个按钮,第一个按钮用来控制其他两个按钮的 disableenable 状态。
MainWindow.xaml.cs

using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace DisableIcon
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         InitializeComponent();
      }

      private void ToggleButton_Click(object sender, RoutedEventArgs e)
      {
         NormalButton.IsEnabled = !NormalButton.IsEnabled;
         NormalButtonImage.Opacity = NormalButton.IsEnabled ? 1.0 : 0.5;
         FormatConvertedBitmap oldFormatConvertedBitmap = IconGrayButtonImage.Source as FormatConvertedBitmap;
         FormatConvertedBitmap newFormatConvertedBitmap = new FormatConvertedBitmap();
         newFormatConvertedBitmap.BeginInit();
         newFormatConvertedBitmap.Source = oldFormatConvertedBitmap.Source;
         newFormatConvertedBitmap.DestinationFormat = NormalButton.IsEnabled ? PixelFormats.Default : PixelFormats.Gray8; // 注意:无法仅仅改变 DestinationFormat 而不重新 initialize,具体原因未知。
         newFormatConvertedBitmap.EndInit();
         IconGrayButtonImage.Source = newFormatConvertedBitmap;
         IconGrayButton.IsEnabled = NormalButton.IsEnabled;
      }
   }
}

MainWindow.xaml

<Window x:Class="DisableIcon.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DisableIcon"
        mc:Ignorable="d"
        Title="MainWindow" Height="240" Width="240">
   <Window.Resources>
      <BitmapImage x:Key="ButtonIcon" UriSource="icon.jpg" />
   </Window.Resources>
    <Grid>
      <StackPanel VerticalAlignment="Center">
         <!-- Normal button with icon -->
         <Button x:Name="ToggleButton" Width="180" Height="32" Margin="10" Click="ToggleButton_Click">
            <StackPanel Orientation="Horizontal">
               <Image Source="{StaticResource ButtonIcon}"></Image>
               <Label Content="Toggle Button"></Label>
            </StackPanel>
         </Button>
         <!-- Normal button with icon & text is disabled -->
         <Button x:Name="NormalButton" Width="180" Height="32" IsEnabled="False" Margin="10">
            <StackPanel Orientation="Horizontal">
               <Image x:Name="NormalButtonImage" Source="{StaticResource ButtonIcon}" Opacity="0.5"></Image>
               <Label Content="Normal Button"></Label>
            </StackPanel>
         </Button>
         <!-- Normal button with icon gray & text is disabled -->
         <Button x:Name="IconGrayButton" Width="180" Height="32" IsEnabled="False" Margin="10">
            <StackPanel Orientation="Horizontal">
               <Image x:Name="IconGrayButtonImage">
                  <Image.Source>
                     <FormatConvertedBitmap Source="{StaticResource ButtonIcon}" DestinationFormat="Gray8">
                     </FormatConvertedBitmap>
                  </Image.Source>
               </Image>
               <Label Content="Gray Button"></Label>
            </StackPanel>
         </Button>
      </StackPanel>
   </Grid>
</Window>

效果

Diable的效果
Diable的效果
Enable的效果
Enable的效果

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值