gpt4 book ai didi

wpf - ToggleButton 动画的启动状态

转载 作者:行者123 更新时间:2023-12-05 00:57:33 26 4
gpt4 key购买 nike

我想为 ToggleButton 创建一个动画“切换”样式(就像在智能手机上一样)。

动画本身不是问题,当我点击它时,开关看起来不错并且动画。但它也会在加载并打开时进行动画处理。

当我加载我的 View 时,它应该显示一些开关打开和一些关闭。但它只显示“关闭”并自动启动“打开”动画。

下面的简单代码显示了我的问题:在选中按钮时,我将 Button 设置为 150px 宽,而不是开关。

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AnimationTest2.MainWindow"
Title="MainWindow" Height="350" Width="525">

<Window.Resources>
<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource {x:Type ToggleButton}}" x:Key="SwitchStyle">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}" Value="true">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard Storyboard.TargetProperty="Width">
<DoubleAnimation To="150"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard Storyboard.TargetProperty="Width">
<DoubleAnimation To="50"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<ToggleButton Width="50" Content="Button" Style="{StaticResource ResourceKey=SwitchStyle}"/>
<ToggleButton Width="50" Content="Button" Style="{StaticResource ResourceKey=SwitchStyle}" IsChecked="True"/>
</StackPanel>
</Window>

第一个 ToggleButton 运行良好。单击时未选中并设置为 150px 的动画。
默认情况下选中第二个 ToggleButton 并且应该立即为 150px,但是当我启动我的应用程序时它开始增长!

不知何故,我需要根据 IsChecked 状态为我的 ToggleButtons 定义一个初始状态,并仅在单击时启动动画。我怎样才能做到这一点?

我也尝试过使用 VisualStateManager,很像 this answer ,但后来我有同样的问题......

更新

这是实际的工作开关,如果有人不仅对概念验证感兴趣:
    <Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource {x:Type ToggleButton}}" x:Key="SwitchToggleButton">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Background" Value="Gray"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Width" Value="70"/>
<Setter Property="Height" Value="30"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid>
<Border x:Name="BackBorder" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}"/>
<TextBlock x:Name="Off" Margin="30,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Center"><Run Text="OFF"/></TextBlock>
<TextBlock x:Name="On" Margin="0,0,30,0" VerticalAlignment="Center" HorizontalAlignment="Center"><Run Text="ON"/></TextBlock>
<Border x:Name="Slider" Background="LightGray" Width="30" Height="30" HorizontalAlignment="Left" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}"/>
</Grid>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Border.Loaded" SourceName="Slider">
<SkipStoryboardToFill BeginStoryboardName="checkedSB" />
<SkipStoryboardToFill BeginStoryboardName="checkedSB2" />
<SkipStoryboardToFill BeginStoryboardName="uncheckedSB" />
<SkipStoryboardToFill BeginStoryboardName="uncheckedSB2" />
</EventTrigger>
<DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}" Value="true">
<DataTrigger.EnterActions>
<BeginStoryboard x:Name="checkedSB">
<Storyboard Storyboard.TargetProperty="Margin" Storyboard.TargetName="Slider">
<ThicknessAnimation To="40,0,0,0" Duration="00:00:00.2"/>
</Storyboard>
</BeginStoryboard>
<BeginStoryboard x:Name="checkedSB2">
<Storyboard Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)">
<ColorAnimation To="LightSeaGreen" Duration="00:00:00.3"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard x:Name="uncheckedSB">
<Storyboard Storyboard.TargetProperty="Margin" Storyboard.TargetName="Slider">
<ThicknessAnimation To="0" Duration="00:00:00.2"/>
</Storyboard>
</BeginStoryboard>
<BeginStoryboard x:Name="uncheckedSB2">
<Storyboard Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)">
<ColorAnimation To="{x:Null}" Duration="00:00:00.3"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

最佳答案

您可以添加 SkipStoryboardToFill对象进入 Loaded EventTrigger .所以代码看起来像这样。

<Style.Triggers>
<EventTrigger RoutedEvent="ToggleButton.Loaded">
<SkipStoryboardToFill BeginStoryboardName="checkedSB" />
<SkipStoryboardToFill BeginStoryboardName="uncheckedSB" />
</EventTrigger>
<DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}" Value="true">
<DataTrigger.EnterActions>
<BeginStoryboard Name="checkedSB">
<Storyboard Storyboard.TargetProperty="Width">
<DoubleAnimation To="150"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard Name="uncheckedSB">
<Storyboard Storyboard.TargetProperty="Width">
<DoubleAnimation To="50"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>

关于wpf - ToggleButton 动画的启动状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34308250/

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