从情节提要更改模板或内容

时间:2019-07-03 11:07:58

标签: c# wpf xaml

我想要可以在ItemsControl中使用的DataTemplate。每个单独的DataTemplate对象都应通过监听DataTriggers来独立设置动画。

此DataTemplate当前看起来像这样:

<DataTemplate x:Key="WindowButton">
        <DataTemplate.Resources>
            <Style TargetType="ContentControl" x:Key="IconControl">
                <Style.Resources>
                    <ContentControl x:Key="DrawerOpened" Content="{DynamicResource DrawerOpenedTemplate}" />
                </Style.Resources>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsOpened}" Value="True">
                        <DataTrigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" RepeatBehavior="1x" Duration="0:0:0.5"/>
                                    <ObjectAnimationUsingKeyFrames Duration="0" BeginTime="0:0:0.5" Storyboard.TargetProperty="Template">
                                        <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{StaticResource DrawerOpened}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" BeginTime="0:0:0.5" Duration="0:0:0.5"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </DataTrigger.EnterActions>
                        <!--<Setter Property="Template" Value="{DynamicResource DrawerOpenedTemplate}" />-->
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IsOpened}" Value="False">
                        <Setter Property="Template" Value="{DynamicResource DrawerClosedTemplate}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IsWorking}" Value="True">
                        <Setter Property="Template" Value="{DynamicResource WaitSpinnerTemplate}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataTemplate.Resources>
        <Button Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.WindowControlClickCommand}" CommandParameter="{Binding }">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="2*"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0" Orientation="Horizontal">
                    <ContentControl Height="50" Width="50" Style="{DynamicResource IconControl}" Margin="5,10,0,0"></ContentControl>
                    <TextBlock HorizontalAlignment="Center" Margin="0,10,5,0" VerticalAlignment="Center" FontSize="30" Text="{Binding WindowLabel}" />
                </StackPanel>
                <TextBlock Grid.Row="1" Style="{StaticResource ActionLabelStyle}" Margin="0,0,0,5" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" />
            </Grid>
         </Button>
    </DataTemplate>

如您所见,我创建的DataTemplate具有内部样式,该样式绑定到某些属性,并且在绑定更改时应该会更改自身。

因此,我创建了带有几个不同动画的情节提要-控件慢慢地淡化为零,然后将模板更改为其他内容,然后又以不同的形式再次出现。一般而言,它运作良好,但我想通过添加动画使其更漂亮。

不幸的是,由于任何原因,Storyboard都无法为无法冻结的事物制作动画-这种特定的ContentControl只是一些XAML描述的矢量图形(Viewbox + Canvas + Path内置),而ControlTemplate中没有包装任何外部引用。

我该如何解决并实现这种描述的行为?

0 个答案:

没有答案