2

I wish to set the opacity of a set of images on my main window to 0.25 when a dependency property on the window itself is false and restore them when the dependency property is true. I have attempted to use a style trigger for this but ran into difficulty when the dependency property was on a different type to the opacity property.

    <Window.Resources>
        <Style x:Key="IconEnabledStyle" TargetType="Image">
            <Style.Triggers>
                <!-- MenuOptionsEnabled is on main window. Opacity is on Image -->
                <Trigger Property="MenuOptionsEnabled" Value="False">
                    <Setter Property="Opacity" Value="0.25"></Setter>
                </Trigger>
                <Trigger Property="MenuOptionsEnabled" Value="True">
                    <Setter Property="Opacity" Value="1.0"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

As you can see the Trigger property is on a different type to the Setter property. Is there a way to define the type for both Trigger and Setter property.

0

1 Answer 1

3

To solve the problem, you can use DataTrigger with RelativeSource to find the ancestor Window, something like this:

<Style x:Key="IconEnabledStyle" TargetType="Image">
    <Style.Triggers>    
       <DataTrigger Binding="{Binding MenuOptionsEnabled, 
                    RelativeSource={RelativeSource AncestorType=Window}}" 
                    Value="False">
          <Setter Property="Opacity" Value="0.25"></Setter>
       </DataTrigger>
    </Style.Triggers>
</Style>

Note that the default Opacity is already 1. So you don't need any Trigger to reset it to 1. When the condition is met, it's set to 0.25, otherwise it will be automatically reset to the value before.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.