Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 165505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:55:58+00:00 2026-05-11T11:55:58+00:00

I have an ItemsControl with Items being added through databinding to an observable collection.

  • 0

I have an ItemsControl with Items being added through databinding to an observable collection. Each item has a data template that defines its look.

I am trying to figure out if it is possible to apply/trigger animations to/on each of the Items in the ItemsControl when the VisualStateManager puts the ItemsControl in a particular state.

Below is a picture – when the items control goes into the closed state – I want the items in the items control to shrink as well as hide the text and have a number become visible. Is this possible using VSM or do I need to attach animations to each item when they are created and then manually kick them off when I want them to change visual state.

alt text http://www.edefine.com/images/misc/drawing1.jpg

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:55:59+00:00Added an answer on May 11, 2026 at 11:55 am

    This is possible using a ObjectAnimationUsingKeyFrames, however it’s stupidly hard to do, will cause you to rip your hair out, crash your visual studio regularly and gives you very little over doing it the simple way.

    The simple way:

    public class TestSwapContentControl : ContentControl  {     object StoredOriginalContent;      public object FullContent     {         get { return (object)GetValue(FullContentProperty); }         set { SetValue(FullContentProperty, value); }     }      // Using a DependencyProperty as the backing store for FullContent.  This enables animation, styling, binding, etc...     public static readonly DependencyProperty FullContentProperty =         DependencyProperty.Register(             'FullContent'             , typeof(object)             , typeof(TestSwapContentControl)             , null);      public void SwitchToFullContent()     {         if (FullContent != null)         {             StoredOriginalContent = Content;             Content = FullContent;         }     }      public void SwitchToNormalContent()     {         if(StoredOriginalContent != null)         {             Content = StoredOriginalContent;         }     } } 

    Then the xaml to use:

        <local:TestSwapContentControl x:Name='mySwitch'>             <Rectangle Height='50' Width='100' Fill='Black' />         <local:TestSwapContentControl.FullContent>                 <StackPanel>                     <TextBlock>1</TextBlock>                     <TextBlock>2</TextBlock>                     <TextBlock>3</TextBlock>                     <TextBlock>4</TextBlock>                     <Rectangle Height='50' Width='100' Fill='Red' />                 </StackPanel>             </local:TestSwapContentControl.FullContent>     </local:TestSwapContentControl>    

    With the following cs in the page:

        private void Button_Click(object sender, RoutedEventArgs e)     {         if (myTempBool)         {             mySwitch.SwitchToFullContent();             myTempBool = false;         }         else         {             mySwitch.SwitchToNormalContent();             myTempBool = true;         }     } 

    Now, if you really need to make the control completely extensible by other developers, you’ll need to use visualstatemenager, but it’s a real bitch. If you don’t know how to set up visual state manager and states through generic.xaml, here’s a how-to guide:

    http://scorbs.com/2008/06/11/parts-states-model-with-visualstatemanager-part-1-of/

    Here’s a working example but it’s not perfect as I can’t seem to set the content of the ContentPresenter directly.

    using System.Windows; using System.Windows.Controls;  namespace SilverlightTestApplication {     [TemplateVisualState(Name='Normal', GroupName='SizeStates')]     [TemplateVisualState(Name='Expanded', GroupName='SizeStates')]     public class TestVSMControl : ContentControl     {         public object SmallContent         {             get { return (object)GetValue(SmallContentProperty); }             set { SetValue(SmallContentProperty, value); }         }          // Using a DependencyProperty as the backing store for SmallContent.  This enables animation, styling, binding, etc...         public static readonly DependencyProperty SmallContentProperty =             DependencyProperty.Register('SmallContent', typeof(object), typeof(TestVSMControl), null);          public object LargeContent         {             get { return (object)GetValue(LargeContentProperty); }             set { SetValue(LargeContentProperty, value); }         }          // Using a DependencyProperty as the backing store for LargeContent.  This enables animation, styling, binding, etc...         public static readonly DependencyProperty LargeContentProperty =             DependencyProperty.Register('LargeContent', typeof(object), typeof(TestVSMControl), null);          public bool Pressed         {             get { return (bool)GetValue(PressedProperty); }             set { SetValue(PressedProperty, value); }         }          // Using a DependencyProperty as the backing store for Pressed.  This enables animation, styling, binding, etc...         public static readonly DependencyProperty PressedProperty =             DependencyProperty.Register('Pressed', typeof(bool), typeof(TestVSMControl),                  new PropertyMetadata(new PropertyChangedCallback(PressedPropertyChanged)));          static void PressedPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)         {             var me = sender as TestVSMControl;             me.ChangeState();         }          public TestVSMControl()         {             DefaultStyleKey = typeof(TestVSMControl);         }          void ChangeState()         {             GoToState(true);         }          private void GoToState(bool useTransitions)         {             if (Pressed)             {                 VisualStateManager.GoToState(this, 'Normal', useTransitions);             }             else             {                 VisualStateManager.GoToState(this, 'Expanded', useTransitions);             }         }     } } 

    In your generic.xaml (include xmlns:vsm=’clr-namespace:System.Windows;assembly=System.Windows’):

    <Style TargetType='local:TestVSMControl'>             <Setter Property='Template'>         <Setter.Value>             <ControlTemplate TargetType='local:TestVSMControl'>                 <StackPanel>                     <vsm:VisualStateManager.VisualStateGroups>                         <vsm:VisualStateGroup x:Name='SizeStates'>                             <vsm:VisualState x:Name='Normal'>                                 <Storyboard>                                     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty='Content' Storyboard.TargetName='myContentPresenter' BeginTime='00:00:00' Duration='00:00:00.0010000' >                                         <ObjectAnimationUsingKeyFrames.KeyFrames>                                             <DiscreteObjectKeyFrame KeyTime='0:0:0'>                                                 <DiscreteObjectKeyFrame.Value>                                                     <StackPanel>                                                         <TextBlock>Rararasputin</TextBlock>                                                         <Button Content='{TemplateBinding SmallContent}' />                                                     </StackPanel>                                                 </DiscreteObjectKeyFrame.Value>                                             </DiscreteObjectKeyFrame>                                         </ObjectAnimationUsingKeyFrames.KeyFrames>                                     </ObjectAnimationUsingKeyFrames>                                 </Storyboard>                             </vsm:VisualState>                             <vsm:VisualState x:Name='Expanded'>                                 <Storyboard>                                     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty='Content' Storyboard.TargetName='myContentPresenter' BeginTime='00:00:00' Duration='00:00:00.0010000' >                                          <ObjectAnimationUsingKeyFrames.KeyFrames>                                             <DiscreteObjectKeyFrame KeyTime='0:0:0'  >                                                 <DiscreteObjectKeyFrame.Value>                                                     <StackPanel>                                                         <TextBlock>Other one</TextBlock>                                                         <Button Content='{TemplateBinding LargeContent}' />                                                     </StackPanel>                                                 </DiscreteObjectKeyFrame.Value>                                             </DiscreteObjectKeyFrame>                                         </ObjectAnimationUsingKeyFrames.KeyFrames>                                     </ObjectAnimationUsingKeyFrames>                                 </Storyboard>                             </vsm:VisualState>                         </vsm:VisualStateGroup>                     </vsm:VisualStateManager.VisualStateGroups>                                             <ContentPresenter x:Name='myContentPresenter' />                 </StackPanel>             </ControlTemplate>         </Setter.Value>     </Setter> </Style> 

    And how to use in your page:

                <local:TestVSMControl x:Name='myVSMControl' Height='200'>                 <local:TestVSMControl.SmallContent>                     <Rectangle Height='50' Width='100' Fill='Red' />                 </local:TestVSMControl.SmallContent>                 <local:TestVSMControl.LargeContent>                     <Rectangle Height='50' Width='100' Fill='Green' />                 </local:TestVSMControl.LargeContent>                     </local:TestVSMControl>             <Button Content='Swap' x:Name='VSMButton' Click='VSMButton_Click'  /> 

    with the following in your page:

        private void VSMButton_Click(object sender, RoutedEventArgs e)     {         myVSMControl.Pressed = !myVSMControl.Pressed;     } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ItemsControl that contains items that each has its own DataTemplate .
I have a itemscontrol that displays some textboxes dependning on the observable collection items
I have an ItemsControl that is bound to a collection of objects. Each object
I have a datagrid bound to an observable collection. Each item inside of the
I have an ItemsControl that is data bound to a list of decimal s.
I have an ItemsControl with a DataTemplate that has been defined. My ItemsControl definition
I have a WPF ItemsControl who's ItemsSource is bound to an observable collection of
I have a collection that holds multiple types of items that all inherit from
On my code i have itemsControl and i want to filter the items that
I have a bound treeview with an ItemsControl that dynamically creates the treeview items

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.