如何在DataTrigger中延迟Setter生效0.5秒?

时间:2010-12-23 18:48:58

标签: wpf wpf-controls binding

我想知道是否有可能延迟数据触发器将布局更改0.5秒。有什么简单的方法吗?我需要设置对象的可见性,但等待0.5秒。任何阿达斯都非常感激。

<DataTemplate x:Key="ListBoxItemDataTemplate">
        <Grid x:Name="DataItem">
            <Image x:Name="IconImage" Source="{Binding XPath=@icon}" Height="16" Margin="16,0,0,0" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Left" />
            <TextBlock x:Name="ListboxIemtextBlock" Text="{Binding XPath=@name}" />
            <Image x:Name="ArrowImage" Height="10" Source="Resources/Images/arrow_collapsed_grey.png" Visibility="{Binding XPath=@state}"/>
        </Grid>
         <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True">
                <Setter TargetName="ListboxIemtextBlock" Property="Foreground" Value="White"/>
                <Setter TargetName="IconImage" Property="Source" Value="{Binding XPath=@iconSelected}"/>
                <Setter TargetName="IconImage" Property="Height" Value="16"/>
                <Setter TargetName="ArrowImage" Property="Source" Value="Resources/Images/arrow_collapsed_white.png"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True">
                <Setter TargetName="ListboxIemtextBlock" Property="Foreground" Value="#FF6dacbe"/>
            </DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}}, Path=SelectedItem.Attributes[retract].Value}" Value="True">      
                <Setter TargetName="ListboxIemtextBlock" Property="Visibility" Value="Hidden" /> 
                <DataTrigger.EnterActions>                     
                    <BeginStoryboard Name="StartAnimation" Storyboard="{StaticResource MakeObjectVisibleAfterHalfASecond}"/>                   
                </DataTrigger.EnterActions>                   
                <DataTrigger.ExitActions>                     
                    <RemoveStoryboard BeginStoryboardName="StartAnimation"/>                   
                </DataTrigger.ExitActions>       
            </DataTrigger> 
         </DataTemplate.Triggers></DataTemplate>

Storyaboard:

<Storyboard x:Key="MakeObjectVisibleAfterHalfASecond" Storyboard.TargetName="ListboxIemtextBlock">         
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Duration="0" BeginTime="0:0:.5">           
            <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" />         
        </ObjectAnimationUsingKeyFrames>       
    </Storyboard> 

1 个答案:

答案 0 :(得分:12)

可以使用动画完成。涉及的部分是:

1)ObjectAnimationUsingKeyFrames在目标上设置Visibility属性,BeginTime0:0:.5,以便在故事板开始时延迟半秒。< / p>

2)DataTrigger检查其更改将使对象可见的属性(在本例中,IsChecked上的CheckBox属性名为Start。< / p>

3)BeginStoryboard DataTrigger.EnterActions启动动画,RemoveStoryboard DataTrigger.ExitActions <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Page.Resources> <Storyboard x:Key="MakeObjectVisibleAfterHalfASecond"> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Duration="0" BeginTime="0:0:.5"> <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" /> </ObjectAnimationUsingKeyFrames> </Storyboard> </Page.Resources> <DockPanel> <CheckBox DockPanel.Dock="Top" Margin="10" x:Name="Start">Check this to make the label appear</CheckBox> <Border BorderThickness="2" BorderBrush="AliceBlue" CornerRadius="5" Margin="10" Padding="10" DockPanel.Dock="Top"> <Label Visibility="Hidden"> <Label.Content>This should appear a half second after the box is checked.</Label.Content> <Label.Style> <Style TargetType="Label"> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=Start, Path=IsChecked}" Value="True"> <DataTrigger.EnterActions> <BeginStoryboard Name="StartAnimation" Storyboard="{StaticResource MakeObjectVisibleAfterHalfASecond}"/> </DataTrigger.EnterActions> <DataTrigger.ExitActions> <RemoveStoryboard BeginStoryboardName="StartAnimation"/> </DataTrigger.ExitActions> </DataTrigger> </Style.Triggers> </Style> </Label.Style> </Label> </Border> <TextBlock/> </DockPanel> </Page> ,如果绑定属性更改,则会再次使对象不可见。

这是一个简单的工作示例:

BeginTime

请注意,您也可以通过省略Duration并在动画上设置{{1}}来执行此操作,因为这两者与关键帧动画基本相同。