Can I make a WPF style take precedence over a local value?

允我心安 提交于 2019-12-19 09:10:38

问题


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.



来源:https://stackoverflow.com/questions/16008484/can-i-make-a-wpf-style-take-precedence-over-a-local-value

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