ToggleButton ControlTemplate 旋转路径有动画或没有
ToggleButton ControlTemplate to rotate Path either with animation or without
在此动画 ToggleButton
ControlTemplate
中,单击它会在更改 IsChecked
时旋转 Path
。
<ControlTemplate x:Key="AnimatedExpanderButtonTemp" TargetType="{x:Type ToggleButton}">
<Border x:Name="ExpanderButtonBorder"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Rectangle Fill="Transparent"/>
<Ellipse x:Name="Circle"
Grid.Column="0"
Stroke="DarkGray"
Fill="White"
Width="15"
Height="15"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
<Path x:Name="Arrow"
Grid.Column="0"
Data="M 1,1.5 L 4.5,5 8,1.5"
Stroke="#FF666666"
StrokeThickness="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RenderTransformOrigin="0.5,0.5">
</Path>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="Arrow"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
To="180"
Duration="0:0:0.4"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="Arrow"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
To="0"
Duration="0:0:0.4"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
但是有些情况下我不想播放动画。
这样当ToggleButton
第一次加载并被预先检查时,
或者,
如果在 VirtualizingPanel
中使用它并且按钮行超出范围并再次返回。
部分场景:
1) 如果它是预先检查的,而不是通过点击,那么它会直接将箭头旋转 180 度而没有动画。
2)如果点击且Checked为True,则旋转180动画。
3)如果点击且Checked为False,则旋转到0动画。
我怎样才能做到这一点?
尝试这样的事情:
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="True" />
<Condition Property="IsPressed" Value="False" />
<!--<Condition Property="IsMouseOver" Value="False" /> not sure if needed in your case-->
</MultiTrigger.Conditions>
<!--your logic for changing without animation-->
<!--<Setter Property="" Value="" />-->
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="True" />
<Condition Property="IsPressed" Value="True" />
<!--<Condition Property="IsMouseOver" Value="False" /> not sure if needed in your case-->
</MultiTrigger.Conditions>
<!--your logic for changing with animation-->
<!--<Setter Property="" Value="" />-->
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="False" />
<Condition Property="IsPressed" Value="True" />
<!--<Condition Property="IsMouseOver" Value="False" /> not sure if needed in your case-->
</MultiTrigger.Conditions>
<!--your logic for changing with animation-->
<!--<Setter Property="" Value="" />-->
</MultiTrigger>
</ControlTemplate.Triggers>
在此动画 ToggleButton
ControlTemplate
中,单击它会在更改 IsChecked
时旋转 Path
。
<ControlTemplate x:Key="AnimatedExpanderButtonTemp" TargetType="{x:Type ToggleButton}">
<Border x:Name="ExpanderButtonBorder"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Rectangle Fill="Transparent"/>
<Ellipse x:Name="Circle"
Grid.Column="0"
Stroke="DarkGray"
Fill="White"
Width="15"
Height="15"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
<Path x:Name="Arrow"
Grid.Column="0"
Data="M 1,1.5 L 4.5,5 8,1.5"
Stroke="#FF666666"
StrokeThickness="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RenderTransformOrigin="0.5,0.5">
</Path>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="Arrow"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
To="180"
Duration="0:0:0.4"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="Arrow"
Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)"
To="0"
Duration="0:0:0.4"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
但是有些情况下我不想播放动画。
这样当ToggleButton
第一次加载并被预先检查时,
或者,
如果在 VirtualizingPanel
中使用它并且按钮行超出范围并再次返回。
部分场景:
1) 如果它是预先检查的,而不是通过点击,那么它会直接将箭头旋转 180 度而没有动画。
2)如果点击且Checked为True,则旋转180动画。
3)如果点击且Checked为False,则旋转到0动画。
我怎样才能做到这一点?
尝试这样的事情:
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="True" />
<Condition Property="IsPressed" Value="False" />
<!--<Condition Property="IsMouseOver" Value="False" /> not sure if needed in your case-->
</MultiTrigger.Conditions>
<!--your logic for changing without animation-->
<!--<Setter Property="" Value="" />-->
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="True" />
<Condition Property="IsPressed" Value="True" />
<!--<Condition Property="IsMouseOver" Value="False" /> not sure if needed in your case-->
</MultiTrigger.Conditions>
<!--your logic for changing with animation-->
<!--<Setter Property="" Value="" />-->
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="False" />
<Condition Property="IsPressed" Value="True" />
<!--<Condition Property="IsMouseOver" Value="False" /> not sure if needed in your case-->
</MultiTrigger.Conditions>
<!--your logic for changing with animation-->
<!--<Setter Property="" Value="" />-->
</MultiTrigger>
</ControlTemplate.Triggers>