开发者

WPF Triggers and Styles

I have a UserControl, in the Resources section I have code like

    <Style TargetType="{x:Type Viewbox}">

        <Style.Triggers>
            <Trigger Property="Viewbox.IsMouseOver" Value="True">
   开发者_运维百科             <Setter Property="Viewbox.Effect" >
                    <Setter.Value>
                        <DropShadowEffect Color="DarkGray" Direction="45" BlurRadius="30"></DropShadowEffect>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

SO this works for the ViewBox that I have in this control,

but when I do

<Viewbox.Triggers>
        <Trigger Property="Viewbox.IsMouseOver" Value="True">
            <Setter Property="Viewbox.Effect" >
                <Setter.Value>
                    <DropShadowEffect></DropShadowEffect>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Viewbox.Triggers>

within the View Box I get errors....

Is there a way to add local triggers to the ViewBox without using a Style?


From MSDN

The collection of triggers established on an element only supports EventTrigger, not property triggers (Trigger). If you require property triggers, you must place these within a style or template and then assign that style or template to the element either directly through the Style property, or indirectly through an implicit style reference.


The Triggers collections of FrameworkElements vs Styles and Templates accept different types of triggers. For FrameworkElement.Triggers you can only use EventTrigger, commonly used to start animations. Style, ControlTemplate, and DataTemplate Triggers collections can use the more common Trigger and DataTrigger like you're using. It's an odd setup that's always sort of felt like an unfinished feature. You could change your code to use a Storyboard to animate properties on the DropShadowEffect from an EventTrigger or just use the Style approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜