WPF XAML更改图像不透明度的IsEnabled状态

6

当IsEnabled为false时,我希望图像的不透明度为0.50。我已经查看了多个示例,但仍然无法掌握如何使其工作。

这是我的自定义控件的完整XAML。非常感谢您的帮助。

<UserControl
 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"
 mc:Ignorable="d"
 x:Class="test.StopButtonControl"
 x:Name="UserControl"
 d:DesignWidth="85" d:DesignHeight="85">

    <Grid x:Name="LayoutRoot">
        <Image x:Name="StopButtonUI" Source="Images/stop.png" Stretch="Fill" MouseUp="StopButtonClick"/>  
    </Grid>
</UserControl>
1个回答

22

你可以通过以下样式触发器将ImageOpacity属性与其IsEnabled属性相结合:

<Grid x:Name="LayoutRoot">
    <Image x:Name="StopButtonUI" Source="Images/stop.png" >
        <Image.Style>
            <Style TargetType="Image">
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Opacity" Value="0.5" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Image.Style>
    </Image>
</Grid>

IsEnabled为false时,这将把Opacity设置为0.5。

UserControlIsEnabled属性因属性继承而改变时,ImageIsEnabled属性将被触发,即图像是用户控件的子元素,因此它也会被设置为IsEnabled属性。


非常感谢,现在我明白了,我会尝试其他属性。 - Nick
1
我在这里有点被Opacity的范围所困扰。对于那些最初认为范围是从0100的人,一定要再次确认你实际上使用的是从01的范围,就像这里演示的一样,否则看起来好像什么也没有改变。 - Reece

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