WPF XAML更改IsEnabled状态下的图像不透明度 [英] WPF XAML Changing Image opacity on IsEnabled state
问题描述
当IsEnabled为false时,我希望图像的不透明度为.50.我一直在看多个示例,但仍然无法理解如何使它工作.
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.
这是我的自定义控件的完整XAML.任何帮助将不胜感激.
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>
推荐答案
您可以通过样式触发器将Image
的Opacity
属性与其IsEnabled
属性耦合,如下所示:
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>
当IsEnabled
为假时,这会将Opacity
设置为0.5.
This will set the Opacity
to 0.5 when IsEnabled
is false.
当UserControl
由于属性继承而更改其IsEnabled
属性时,将触发Image
的IsEnabled
属性,即图像是用户控件的子级,因此其IsEnabled
属性设置也是如此.
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.
这篇关于WPF XAML更改IsEnabled状态下的图像不透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!