WPF XAML Changing Image opacity on IsEnabled state

家住魔仙堡 提交于 2019-12-23 13:25:09

问题


I would like to have the image to have an opacity of .50 when the IsEnabled is false. I have been looking at multiple examples but still I am not able to grasp how to make it work.

Here is the full XAML of my custom control. Any help would be deeply appreciated.

<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:


You can couple the Image's Opacity property to its IsEnabled property via a style trigger as follows:

<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>

This will set the Opacity to 0.5 when IsEnabled is false.

The Image's IsEnabled property will be triggered when the UserControl has its IsEnabled property changed as a result of property inheritance i.e. the image is a child of the user control so it will have its IsEnabled property set too.



来源:https://stackoverflow.com/questions/4565483/wpf-xaml-changing-image-opacity-on-isenabled-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!