Can I make a WPF style take precedence over a loca

2020-03-24 08:39发布

问题:

I have created a boolean attached property in my WPF project, and I want elements with this property set to true to display a certain way, regardless of any local local values that have been set in the XAML.

From the documentation, I understand that by default, local values take precedence over both style setters and style triggers. Is it possible to create a style trigger that takes precedence over local values?

回答1:

Using the technique mentioned by Erti-Chris Eelmaa in his comment on my question, I ended up creating an animation which did the trick:

<Style TargetType="{x:Type Button}">
    <Style.Triggers>
        <Trigger Property="my:Ext.IsReadOnly" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard Name="IsReadOnly">
                    <Storyboard>
                        <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(Button.IsEnabled)" Duration="0:0:0">
                            <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:0" />
                        </BooleanAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
            <Trigger.ExitActions>
                <RemoveStoryboard BeginStoryboardName="IsReadOnly" />
            </Trigger.ExitActions>
        </Trigger>
    </Style.Triggers>
</Style>

This takes precedence over any local value for Button.IsEnabled.



标签: wpf xaml styles