gpt4 book ai didi

wpf - 从 ListBoxItem 访问 Storyboard(资源)

转载 作者:行者123 更新时间:2023-12-02 05:03:34 25 4
gpt4 key购买 nike

我有一个问题。当我单击 ListBoxItem(来自 XAML 而不是 CS)时,如何启动在资源中创建的 Storyboard?

我在“ListBoxItemStyle1”中使用 EventTrigger 进行了测试,但无法访问 Storyboard,未找到。

这是我的代码:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="2,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Selected">
<BeginStoryboard>
<!--?????????????????????????????????????????????-->
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<Storyboard x:Key="MyStoryBoard">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="rectangle">
<EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="164"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="rectangle">
<EasingDoubleKeyFrame KeyTime="0:0:0.7" Value="-1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>

<Grid x:Name="LayoutRoot">
<ListBox HorizontalAlignment="Left" Height="56" Margin="59,39,0,0" VerticalAlignment="Top" Width="147" ItemContainerStyle="{DynamicResource ListBoxItemStyle1}"/>
<Rectangle x:Name="rectangle" Fill="Red" HorizontalAlignment="Left" Height="87" Margin="59,130,0,0" Stroke="Black" VerticalAlignment="Top" Width="147" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
</Grid>

最佳答案

如果您想在事件中调用 Storyboard,请将其作为资源并使用 StaticResource 来调用它。考虑到所有资源都在 App.xaml 中定义

<Application.Resources>
<Storyboard x:Key="MyStoryboard">
....
</Storyboard>
</Application.Resources>

然后从选定的事件中调用它。

...

<EventTrigger RoutedEvent="Selected">
<BeginStoryboard Storyboard="{StaticResource MyStoryboard}"
x:Name="begin_MyStoryboard"/>
</EventTrigger>

希望对它有帮助

关于wpf - 从 ListBoxItem 访问 Storyboard(资源),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14010538/

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