我想知道是否有可能延迟数据触发器以每秒更改0.5个布局。有什么简单的方法吗?我需要设置对象的可见性,但要等待0.5秒。任何adeas都受到高度赞赏。

<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>

Storyboard:
<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)一个ObjectAnimationUsingKeyFrames,它在目标上设置Visibility属性,并在 Storyboard 开始时将其BeginTime0:0:.5延迟半秒。

2)一个DataTrigger,用于检查其更改将使该对象可见的属性(在本例中为IsCheckedCheckBox上的Start属性)。

3)启动动画的BeginStoryboard中的DataTrigger.EnterActionsRemoveStoryboard中的DataTrigger.ExitActions,如果绑定(bind)的属性改回,它会使对象再次不可见。

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

<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来执行此操作,因为对于关键帧动画,两者本质上是相同的。

关于wpf - 如何在DataTrigger中延迟Setter生效0.5秒钟?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4521574/

10-16 22:03