Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Begin a storyboard in a ControlTemplate

Begin a storyboard in a ControlTemplate

Scheduled Pinned Locked Moved WPF
csharpcssvisual-studiowpfcom
7 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kenneth Haugland
    wrote on last edited by
    #1

    So I was reading the documentation about Prism 7: Composing the User Interface Using the Prism Library 5.0 for WPF[^] and downloaded the source code for the application here: Prism for WPF Reference Implementation Code Sample in C# for Visual Studio 2013[^] I really liked the animated TabControl and thought I'd implement it based on that application. The trouble was that when I implemented it on a different new solution I ran into a problem with the animated Template.

        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Padding" Value="4,4,4,4"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="BorderBrush" Value="{StaticResource TabControlNormalBorderBrush}"/>
        <Setter Property="Background" Value="#F9F9F9"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Controls:AnimatedTabControl">
    
                    <Grid ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="\*"/>
                        </Grid.RowDefinitions>
    
                        <Border Height="30" Margin="10,0,10,10"  HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" Grid.Row="1" CornerRadius="12,12,12,12" Background="{TemplateBinding Background}" BorderThickness="2,2,2,2" BorderBrush="#FFFFFFFF">
                            <TabPanel x:Name="HeaderPanel" HorizontalAlignment="Center" VerticalAlignment="Center" IsItemsHost="true" Grid.Column="0" Grid.Row="0" KeyboardNavigation.TabIndex="1"/>
                        </Border>
    
                        <Grid Grid.Row="1" Margin="0,40,0,0" Horizonta</x-turndown>
    
    P 2 Replies Last reply
    0
    • K Kenneth Haugland

      So I was reading the documentation about Prism 7: Composing the User Interface Using the Prism Library 5.0 for WPF[^] and downloaded the source code for the application here: Prism for WPF Reference Implementation Code Sample in C# for Visual Studio 2013[^] I really liked the animated TabControl and thought I'd implement it based on that application. The trouble was that when I implemented it on a different new solution I ran into a problem with the animated Template.

          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
          <Setter Property="Padding" Value="4,4,4,4"/>
          <Setter Property="BorderThickness" Value="1"/>
          <Setter Property="BorderBrush" Value="{StaticResource TabControlNormalBorderBrush}"/>
          <Setter Property="Background" Value="#F9F9F9"/>
          <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
          <Setter Property="VerticalContentAlignment" Value="Stretch"/>
          <Setter Property="Template">
              <Setter.Value>
                  <ControlTemplate TargetType="Controls:AnimatedTabControl">
      
                      <Grid ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
                          <Grid.RowDefinitions>
                              <RowDefinition Height="Auto"/>
                              <RowDefinition Height="\*"/>
                          </Grid.RowDefinitions>
      
                          <Border Height="30" Margin="10,0,10,10"  HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" Grid.Row="1" CornerRadius="12,12,12,12" Background="{TemplateBinding Background}" BorderThickness="2,2,2,2" BorderBrush="#FFFFFFFF">
                              <TabPanel x:Name="HeaderPanel" HorizontalAlignment="Center" VerticalAlignment="Center" IsItemsHost="true" Grid.Column="0" Grid.Row="0" KeyboardNavigation.TabIndex="1"/>
                          </Border>
      
                          <Grid Grid.Row="1" Margin="0,40,0,0" Horizonta</x-turndown>
      
      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      The problem you're facing is that you cannot have a Binding inside a storyboard. Fortunately, you might be able to leverage the technique here[^] to help solve this.

      This space for rent

      K 1 Reply Last reply
      0
      • P Pete OHanlon

        The problem you're facing is that you cannot have a Binding inside a storyboard. Fortunately, you might be able to leverage the technique here[^] to help solve this.

        This space for rent

        K Offline
        K Offline
        Kenneth Haugland
        wrote on last edited by
        #3

        WPF won't let me play today, I can't seem to make the trick work. However, I have to say that not being able to make bindings on a ControlTemplate animation seems to be quite the limitation. What do pros do to make this work? Code behind?

        P 1 Reply Last reply
        0
        • K Kenneth Haugland

          WPF won't let me play today, I can't seem to make the trick work. However, I have to say that not being able to make bindings on a ControlTemplate animation seems to be quite the limitation. What do pros do to make this work? Code behind?

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          I would use a Behavior to solve this.

          This space for rent

          1 Reply Last reply
          0
          • K Kenneth Haugland

            So I was reading the documentation about Prism 7: Composing the User Interface Using the Prism Library 5.0 for WPF[^] and downloaded the source code for the application here: Prism for WPF Reference Implementation Code Sample in C# for Visual Studio 2013[^] I really liked the animated TabControl and thought I'd implement it based on that application. The trouble was that when I implemented it on a different new solution I ran into a problem with the animated Template.

                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                <Setter Property="Padding" Value="4,4,4,4"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="BorderBrush" Value="{StaticResource TabControlNormalBorderBrush}"/>
                <Setter Property="Background" Value="#F9F9F9"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Controls:AnimatedTabControl">
            
                            <Grid ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="\*"/>
                                </Grid.RowDefinitions>
            
                                <Border Height="30" Margin="10,0,10,10"  HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" Grid.Row="1" CornerRadius="12,12,12,12" Background="{TemplateBinding Background}" BorderThickness="2,2,2,2" BorderBrush="#FFFFFFFF">
                                    <TabPanel x:Name="HeaderPanel" HorizontalAlignment="Center" VerticalAlignment="Center" IsItemsHost="true" Grid.Column="0" Grid.Row="0" KeyboardNavigation.TabIndex="1"/>
                                </Border>
            
                                <Grid Grid.Row="1" Margin="0,40,0,0" Horizonta</x-turndown>
            
            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            Right, I thought I'd give the original implementation a run out to see what your problem might be. The first thing I did (once I'd created a Wpf application ready to test this), was to create AnimatedTabControl.cs. It contains this code:

            using System;
            using System.Windows;
            using System.Windows.Controls;
            using System.Windows.Threading;

            namespace WpfTabSample
            {
            public class AnimatedTabControl : TabControl
            {
            public static readonly RoutedEvent SelectionChangingEvent = EventManager.RegisterRoutedEvent(
            "SelectionChanging", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(AnimatedTabControl));

                private DispatcherTimer timer;
            
                public AnimatedTabControl()
                {
                    DefaultStyleKey = typeof(AnimatedTabControl);
                }
            
                public event RoutedEventHandler SelectionChanging
                {
                    add { AddHandler(SelectionChangingEvent, value); }
                    remove { RemoveHandler(SelectionChangingEvent, value); }
                }
            
                protected override void OnSelectionChanged(SelectionChangedEventArgs e)
                {
                    this.Dispatcher.BeginInvoke(
                        (Action)delegate
                        {
                            this.RaiseSelectionChangingEvent();
            
                            this.StopTimer();
            
                            this.timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 0, 500) };
            
                            EventHandler handler = null;
                            handler = (sender, args) =>
                            {
                                this.StopTimer();
                                base.OnSelectionChanged(e);
                            };
                            this.timer.Tick += handler;
                            this.timer.Start();
                        });
                }
            
                // This method raises the Tap event 
                private void RaiseSelectionChangingEvent()
                {
                    var args = new RoutedEventArgs(SelectionChangingEvent);
                    RaiseEvent(args);
                }
            
                private void StopTimer()
                {
                    if (this.timer != null)
                    {
                        this.timer.Stop();
                        this.timer = null;
                    }
                }
            }
            

            }

            Next I added in a XAML usercontrol, called RoundedBox. It looks like this:

            <UserControl x:Class="WpfTabSample.RoundedBox"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.ope

            K 1 Reply Last reply
            0
            • P Pete OHanlon

              Right, I thought I'd give the original implementation a run out to see what your problem might be. The first thing I did (once I'd created a Wpf application ready to test this), was to create AnimatedTabControl.cs. It contains this code:

              using System;
              using System.Windows;
              using System.Windows.Controls;
              using System.Windows.Threading;

              namespace WpfTabSample
              {
              public class AnimatedTabControl : TabControl
              {
              public static readonly RoutedEvent SelectionChangingEvent = EventManager.RegisterRoutedEvent(
              "SelectionChanging", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(AnimatedTabControl));

                  private DispatcherTimer timer;
              
                  public AnimatedTabControl()
                  {
                      DefaultStyleKey = typeof(AnimatedTabControl);
                  }
              
                  public event RoutedEventHandler SelectionChanging
                  {
                      add { AddHandler(SelectionChangingEvent, value); }
                      remove { RemoveHandler(SelectionChangingEvent, value); }
                  }
              
                  protected override void OnSelectionChanged(SelectionChangedEventArgs e)
                  {
                      this.Dispatcher.BeginInvoke(
                          (Action)delegate
                          {
                              this.RaiseSelectionChangingEvent();
              
                              this.StopTimer();
              
                              this.timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 0, 500) };
              
                              EventHandler handler = null;
                              handler = (sender, args) =>
                              {
                                  this.StopTimer();
                                  base.OnSelectionChanged(e);
                              };
                              this.timer.Tick += handler;
                              this.timer.Start();
                          });
                  }
              
                  // This method raises the Tap event 
                  private void RaiseSelectionChangingEvent()
                  {
                      var args = new RoutedEventArgs(SelectionChangingEvent);
                      RaiseEvent(args);
                  }
              
                  private void StopTimer()
                  {
                      if (this.timer != null)
                      {
                          this.timer.Stop();
                          this.timer = null;
                      }
                  }
              }
              

              }

              Next I added in a XAML usercontrol, called RoundedBox. It looks like this:

              <UserControl x:Class="WpfTabSample.RoundedBox"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:mc="http://schemas.ope

              K Offline
              K Offline
              Kenneth Haugland
              wrote on last edited by
              #6

              It's working now, thank you so much. :) I had implemented the two first items but I got my error in the XAML file so I just posted that. The pictures you commented out just creates some nice shader effects. But, I'm not quite sure why It didn't work for me. :doh:

              P 1 Reply Last reply
              0
              • K Kenneth Haugland

                It's working now, thank you so much. :) I had implemented the two first items but I got my error in the XAML file so I just posted that. The pictures you commented out just creates some nice shader effects. But, I'm not quite sure why It didn't work for me. :doh:

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                I'm pleased you got it working.

                This space for rent

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups