Skip to main content

Microsoft Silverlight

Answered Question How to run annimation on control in ListBox DataTemplateRSS Feed

(0)

cbeck
cbeck

Member

Member

0 points

14 Posts

How to run annimation on control in ListBox DataTemplate

Thanks to all for help on this so far! One more challenge to go. 

I have a structure like the below. I would like to use an animation to change Visibility of "lbInner" to Visible when the mouse enters tbOuter and Collapsed when the mouse leaves tbOuter.

Using the VisualTreeHelper to walk the tree I can find lbInner and control the visibility within the mouse events. What I can't get to work is an animation with lbInner as the Target.
> I created a StoryBoard as a resource and tried running it (myStoryBoard.Begin()), but it reports "cannot find lbInner".
> I added states in Blend and tried using VisualSateManager. It returns false. I assume because it cannot find lbInner.

Here is a brief outline of the structure (full XAML is below)
<ListBox x:Name="lbOuter">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel x:Name="InnerStackPanel">
        <TextBlock x:Name="tbOuter"
                         MouseEnter="tbOuter_MouseEnter"
                         MouseLeave="tbOuter_MouseLeave"/>
        <ListBox x:Name="lbInner" Visibility="Collapsed">
            ...
        </ListBox>
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

Here is what the User Control looks like: http://cbeckPhoto.zenfolio.com/p294334137/e1bd43d6c

Your thoughts on this are greatly appreciated!

Below is the full XAML.

<UserControl ...>
  <Grid x:Name="LayoutRoot">
    <StackPanel Background="LightGray">      
      <ListBox x:Name="lbOuter" ItemsSource="{Binding}" Background="LightBlue" MinHeight="50">
        <ListBox.ItemTemplate>          <DataTemplate>            <StackPanel x:Name="InnerStackPanel" Background="#FFFFFFFF">
              <!-- Had Visual States here. Could not get them to work when calling VSM -->
              <StackPanel.Resources>                <Storyboard x:Name="lbInnerExpand">                  <ObjectAnimationUsingKeyFrames BeginTime="00:00:00"                                      Storyboard.TargetName="lbInner"                                      Storyboard.TargetProperty="(UIElement.Visibility)">                    <DiscreteObjectKeyFrame KeyTime="00:00:00.2000000">                      <DiscreteObjectKeyFrame.Value>                        <Visibility>Visible</Visibility>                      </DiscreteObjectKeyFrame.Value>                    </DiscreteObjectKeyFrame>                  </ObjectAnimationUsingKeyFrames>                </Storyboard>              </StackPanel.Resources>              <TextBlock Text="{Binding}" Foreground="#FF000000" x:Name="tbInner"                         MouseEnter="tbOuter_MouseEnter" MouseLeave="tbOuter_MouseLeave"/>              <ListBox x:Name="lbInner" ItemsSource="{Binding}" Background="#FF90EE90" Foreground="#FF000000"  
                       Visibility
="Collapsed">                <ListBox.ItemsPanel>                  <ItemsPanelTemplate>                    <controls:WrapPanel/>                  </ItemsPanelTemplate>                </ListBox.ItemsPanel>                <ListBox.ItemTemplate>                          <DataTemplate>                      <Grid x:Name="ItemGrid" Height="20" Width="Auto" IsHitTestVisible="True">                        <Grid.ColumnDefinitions>                          <ColumnDefinition Width="14"/>                          <ColumnDefinition Width="*"/>                        </Grid.ColumnDefinitions>                        <Button Content="X" Grid.Column="0" HorizontalAlignment="Stretch"                           VerticalAlignment="Stretch" Cursor="Hand" Click="Button1_Click"/>                        <TextBlock x:Name="DocTagListTagTextBlock" Text="{Binding}" Margin="0,-0.35,0,0.35"                          Grid.Column="1" VerticalAlignment="Center"/>                      </Grid>                  </DataTemplate>                </ListBox.ItemTemplate>                    </ListBox>            </StackPanel>                                                    </DataTemplate>        </ListBox.ItemTemplate>                                                </ListBox>      <TextBlock Text="TextBlock" TextWrapping="Wrap" x:Name="OutputTextBlock"/>      <Button Content="Button" x:Name="TestButton" Click="TestButton_Click"/>    </StackPanel>  </Grid>

</UserControl>

Mog Liang - MSFT
Mog Lian...

All-Star

All-Star

15900 points

1,544 Posts

Answered Question

Re: How to run annimation on control in ListBox DataTemplate

A better way to do this is making tbOuter to be a control derived from Control, then you can use VisualStateManager to implement animation.

I used VisualStateManager in UserControl and seems not work, but in control it works correct.

for more information about Control, refer to this link

http://msdn.microsoft.com/en-us/library/cc278064(VS.95).aspx

Mog Liang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

cbeck
cbeck

Member

Member

0 points

14 Posts

Re: How to run annimation on control in ListBox DataTemplate

Thank you Mog! - I got that working today.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities