我有一个开始崩溃的网格。当用户单击该按钮时,后面的代码将可见性设置为可见。然后按钮XAML触发器使网格在3秒内从0增长到1925年。
我试图弄清楚如何在XAML中对触发器进行编程以查找网格的可见性。然后根据其状态使动画增长或缩小。因此,基于下面的代码,如果触发器确定可见性可见,则我想使网格从1925变为0,如果网格在按钮点击时显示折叠,则为0到1925。想要远离代码,知道UI中发生了什么。这就是为什么稍后我将网格可见性绑定到属性。
XAML的主要组成部分
<Grid Name="gridDisplay" Background="AliceBlue" Visibility="Collapsed">
</Grid>
<Button
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
Width="32" Height="32" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Right" Background="White" Click="Button_Click">
>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="gridDisplay"
Storyboard.TargetProperty="Width"
From="0" To="1925" Duration="0:0:3" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
按钮事件
//TODO: Bind grid to visibility property
if (gridDisplay.Visibility == System.Windows.Visibility.Collapsed)
{
gridDisplay.Visibility = System.Windows.Visibility.Visible;
(sender as Button).Content = "<";
}
else
{
gridDisplay.Visibility = System.Windows.Visibility.Collapsed;
(sender as Button).Content = ">";
}
答案 0 :(得分:5)
不是使用触发器,而是可以制作这样的动画。
xaml:
<Window.Resources>
<Storyboard x:Key="open">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="gridDisplay">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="900"/>
</DoubleAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="gridDisplay">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="close">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="gridDisplay">
<EasingDoubleKeyFrame KeyTime="0" Value="900"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="gridDisplay">
<DiscreteObjectKeyFrame KeyTime="0:0:0.6" Value="{x:Static Visibility.Collapsed}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
buton事件:
//TODO: Bind grid to visibility property
if (gridDisplay.Visibility == System.Windows.Visibility.Collapsed)
{
var storyboard = this.Resources["open"] as Storyboard;
storyboard.Begin();
(sender as Button).Content = "<";
}
else
{
var storyboard = this.Resources["close"] as Storyboard;
storyboard.Begin();
(sender as Button).Content = ">";
}
我希望这会对你有所帮助。 如果你想拥有一个可见的属性,你可以拥有:
private bool _visibleProp;
public bool VisibleProp{get{return _visibleProp;}
set{_visibleProp = value;
if(value){var storyboard = this.Resources["open"] as Storyboard;
storyboard.Begin(); }
else{var storyboard = this.Resources["close"] as Storyboard;
storyboard.Begin();}
}}