gpt4 book ai didi

c# - 基于网格可见性的 WPF 动画

转载 作者:太空宇宙 更新时间:2023-11-03 20:04:33 26 4
gpt4 key购买 nike

我有一个开始折叠的网格。当用户单击按钮时,后面的代码将可见性设置为可见。然后按钮 XAML 触发器使网格在 3 秒内从 0 增长到 1925。

我试图弄清楚如何在 XAML 中对触发器进行编程以查找网格的可见性。然后根据其状态使动画增长或收缩。因此,基于下面的代码,如果触发器确定可见性可见,我想让网格从 1925 变为 0,如果单击按钮时网格显示折叠,则为 0 到 1925。想要远离知道 UI 中发生了什么的代码。这就是为什么稍后我会将网格可见性绑定(bind)到一个属性。

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">
&gt;

<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 = ">";
}

最佳答案

您可以制作这样的动画,而不是使用触发器。

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>

按钮事件:

//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();}
}}

关于c# - 基于网格可见性的 WPF 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24591105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com