图像上的“禁用”效果

3

在 Win Forms 中,当我禁用一个按钮时,它的图像背景会转换为灰色级别图像。如何使用 XAML 代码在 Image 控件中模拟此效果?

3个回答

3

您可以按照以下方式设置按钮中图像的透明度:

<Button>
  <Image Source="button.png">
    <Image.Style>
      <Style TargetType="Image">
        <Style.Triggers>
          <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Opacity" Value="0.4" />
          </Trigger>
        </Style.Triggers>
      </Style>
    </Image.Style>
  </Image>
</Button>

3
为了禁用 Winform 样式,您需要执行类似以下的操作。
  <Button Click="Button_Click">
        <Image Width="154" Height="87" >
            <Image.Style>
                <Style TargetType="Image">
                    <Style.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Source">
                                <Setter.Value>
                                    <FormatConvertedBitmap DestinationFormat="Gray32Float">
                                        <FormatConvertedBitmap.Source>
                                            <BitmapImage UriSource="1.png" />
                                        </FormatConvertedBitmap.Source>
                                    </FormatConvertedBitmap>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="True">
                            <Setter Property="Source">
                                <Setter.Value>                                        
                                            <BitmapImage UriSource="1.png" />                                          
                                </Setter.Value>
                            </Setter>
                        </Trigger>                     
                    </Style.Triggers>                                          
                </Style>
            </Image.Style>
        </Image>
    </Button>

希望这可以帮助:

1
+1 是为了设置 DestinationFormat,但是设置 OpacityMask 有什么必要吗?只设置源不足吗? - Rohit Vats
只是为了使其类似于WinForm样式。 - BRAHIM Kamel

2
请查看greyableimage.codeplex.com
这可以用来替代按钮、菜单、工具栏等上的普通图像控件。它会自动生成一个“灰色”版本的内容,在父控件被禁用时显示。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接