Is it possible to share Storyboards using App.xaml?

jennyliu835 221 Reputation points
2020-09-09T12:02:10.657+00:00

Hi Peter + Daisy,
Is it possible to share Storyboards using App.xaml?
The result of WpfApp2.exe in WpfApp_Animation is preferable.
WpfApp2_ViewModel is what I learned from you about View Model, but I can't implement some of the functionalities in WpfApp_Animation using View Model, such as Storyboard, Hide_TextBlock() and assigning #FFBF72FF.
Thanks a lot.

Developer technologies | Windows Presentation Foundation
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2020-09-10T18:54:57.097+00:00

    Hi Jenny,
    in App.xaml (ResourceDictionary) inside DataTemplate you can refer to Storyboard (in App.xaml - ResourceDictionary).

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:WpfApp2.general_pages"
                        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
    
      <Storyboard x:Key="StoryboardTB1">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="TB1">
          <SplineDoubleKeyFrame KeyTime="0" Value="0"/>
          <SplineDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
      </Storyboard>
    
      <Storyboard x:Key="StoryboardTB2">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="TB2">
          <SplineDoubleKeyFrame KeyTime="0" Value="0"/>
          <SplineDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
      </Storyboard>
    
      <Storyboard x:Key="StoryboardTB3">
        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)" Storyboard.TargetName="TB3">
          <SplineColorKeyFrame KeyTime="0" Value="#FFFDD293"/>
          <SplineColorKeyFrame KeyTime="0:0:6" Value="#FFBF72FF"/>
        </ColorAnimationUsingKeyFrames>
      </Storyboard>
    
      <DataTemplate x:Key="insertion">
        <Grid HorizontalAlignment="Center" 
              DataContext="{Binding RelativeSource={RelativeSource AncestorType=Page, Mode=FindAncestor}, Path=DataContext}">
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"  />
            <ColumnDefinition Width="100"  />
          </Grid.ColumnDefinitions>
    
          <StackPanel Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,10,20,0" >
            <!--<Button Content="Preference" Width="80" Command="{Binding}" CommandParameter="Preference"/>-->
            <Button Content="General" Width="65" Command="{Binding}" CommandParameter="General">
              <Button.Triggers>
                <EventTrigger RoutedEvent="PreviewMouseLeftButtonDown">
                  <BeginStoryboard Storyboard="{StaticResource StoryboardTB1}"/>
                </EventTrigger>
              </Button.Triggers>
            </Button>
            <Button Content="Primary" Width="65" Margin="0,10,0,0" Command="{Binding}" CommandParameter="Primary"/>
            <Button Content="Secondary" Width="65" Margin="0,10,0,0" Command="{Binding}" CommandParameter="Secondary"/>
          </StackPanel>
          <TextBlock x:Name="TB1" Text="Colors" FontSize="16" Height="78" Width="190" Background="#FFC9C9C9" 
                     Visibility="{Binding Tb1Visibility}" HorizontalAlignment="Left" Margin="20,0,0,0"/>
        </Grid>
      </DataTemplate>
    
    </ResourceDictionary>
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.