wpf中的触发器

StyleControlTemplateDataTemplate 都具有 Triggers 属性,该属性可以包含一组触发器。某个属性值更改时,或某个事件引发时,触发器会相应地设置属性或启动操作(如动画操作)。

这里说一个简单属性触发器

在window中添加一个listbox
 <ListBox Name="l1" >
            <ListBoxItem>1111</ListBoxItem>
            <ListBoxItem>2222</ListBoxItem>
            <ListBoxItem>3333</ListBoxItem>
            <ListBoxItem>4444</ListBoxItem>
        </ListBox>

然后在Window.Resources中添加一个触发器。触发器也是在样式中定义的
刚开始listbox中各个项目透明度为01.选择用户透明度为1.0
<Style TargetType="ListBoxItem">
            <Setter Property="Opacity" Value="0.1" />
            <Setter Property="MaxHeight" Value="75" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Opacity" Value="1.0" />
                </Trigger>
            </Style.Triggers>
        </Style>
下面说说事件触发器
EventTrigger 和 Storyboard

它根据事件的引发来启动一组操作。下面的 EventTrigger 对象指定当鼠标指针进入 ListBoxItem 时,MaxHeight 属性在 0.2 秒时间内以动画方式增大为值 90。当鼠标离开该项时,该属性在 1 秒时间内还原为原始值

<EventTrigger RoutedEvent="Mouse.MouseEnter"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="MaxHeight" To="90" /> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> <EventTrigger RoutedEvent="Mouse.MouseLeave"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <DoubleAnimation Duration="0:0:1" Storyboard.TargetProperty="MaxHeight" /> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger>

本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。

posted @ 2010-02-26 19:37  音乐啤酒  阅读(243)  评论(0编辑  收藏  举报