【发布时间】:2017-12-05 12:33:24
【问题描述】:
我有一个 StackPanel 和一个按钮。 Initallty 按钮内容应为 > 正确发生折叠的位置。再次按下同一个按钮时,我希望显示内容并且文本应该是
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" x:Name="stkEasyLocatePanel" Loaded="stkEasyLocatePanel_Loaded" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="0,0,0,28"></StackPanel>
<Button Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,5,0,0" Panel.ZIndex="140" Height="20" Width="25" Background="Transparent" Content="<<">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard x:Name="HideStackPanel">
<DoubleAnimation Storyboard.TargetName="stkEasyLocatePanel" Storyboard.TargetProperty="Width" From="330" To="0" Duration="0:0:0.3">
<DoubleAnimation.EasingFunction>
<PowerEase EasingMode="EaseIn"></PowerEase>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
<BeginStoryboard>
<!--This part dosent work. The content collapses and shows on a single click. But I want it to happen on two clicks of same button-->
<Storyboard x:Name="ShowStackPanel">
<DoubleAnimation Storyboard.TargetName="stkEasyLocatePanel" Storyboard.TargetProperty="Width" From="0" To="330" Duration="0:0:0.3">
<DoubleAnimation.EasingFunction>
<PowerEase EasingMode="EaseIn"></PowerEase>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
</Grid>
【问题讨论】:
-
您希望它在纯 xaml 中,您更喜欢使用 Triggers 还是 VisualStateManager?
-
两者都可以。但我想知道为普通按钮执行此操作的解决方案。
-
为什么普通按钮很重要?如有必要,您可以设置一个 Toggle 来模仿普通按钮的设计/行为,@AnjumSKhan 的答案是正确的选择。要仅使用普通按钮,您需要在后面的代码中处理条件表达式,在我看来,纯 xaml 方法在这种情况下就可以了。
-
哦,知道了。是的切换按钮工作正常。我想知道是否有办法使用纯 xaml 使用普通按钮解决问题。但如果它涉及到代码,那么我猜 Toggle Button 是最好的方法。