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

Nic*_*ick 4 wpf xaml user-controls opacity isenabled

当IsEnabled为false时,我希望图像的不透明度为.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>
Run Code Online (Sandbox Code Playgroud)

Tim*_*oyd 19

您可以结合的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>
Run Code Online (Sandbox Code Playgroud)

Opacity如果IsEnabled为false,则将设置为0.5 .

ImageIsEnabled属性时,将触发UserControl了它的IsEnabled性质改变财产继承的结果,即图像是用户控制的孩子,因此将有其IsEnabled属性设置了.