2014-01-27 15 views
0

我正在開發一個應用程序,我需要爲用戶敲擊時製作的控件的高度設置動畫。這個控件在名爲「taskListBox」的ListBox中。這是我的代碼:C#中定製控件的動畫高度

 private void taskListBox_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
    { 
     // upcomingLongListSelector was tapped, open the item or select the item 
     if (editMode.Equals(false)) 
     { 
      try 
      { 
       var current = taskListBox.SelectedItem as TaskItem; 
       String selectedItem = current.tasknameTextBlock.Text.ToString(); 

       NavigationService.Navigate(new Uri("/taskPage.xaml?key=" + selectedItem, UriKind.Relative)); 
      } 
      catch (Exception) 
      { 
      } 
     } 
     else if (editMode.Equals(true)) 
     { 
      // Expand the task 
      var selectedItem = taskListBox.SelectedItem as TaskItem; 

      Storyboard sb = new Storyboard(); 
      DoubleAnimation animation = new DoubleAnimation { From = 0, To = 150, Duration = TimeSpan.FromSeconds(0.5) }; 
      Storyboard.SetTarget(animation, selectedItem); 
      Storyboard.SetTargetProperty(animation, new PropertyPath(Height)); 
      sb.Children.Add(animation); 
      sb.Begin(); 
     } 
    } 

我的控制:

<UserControl x:Class="App.TaskItem" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" Height="76" Width="359" Background="Black"> 

<Grid x:Name="LayoutRoot"> 
    <Grid.Background> 
     <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
      <GradientStop Color="#FF092949" Offset="0"/> 
      <GradientStop Color="#FF06192C" Offset="1"/> 
     </LinearGradientBrush> 
    </Grid.Background> 
    <TextBlock x:Name="tasknameTextBlock" HorizontalAlignment="Left" Margin="10,7,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="26"/> 
    <TextBlock x:Name="locationTextBlock" HorizontalAlignment="Left" Margin="9,44,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20"/> 
    <Image x:Name="checkImage" HorizontalAlignment="Left" Margin="307,10,0,0" Width="47" Source="/Green check.png" Stretch="None" Visibility="Collapsed"/> 
</Grid> 

,但此行拋出一個NullReferenceException:

Storyboard.SetTargetProperty(animation, new PropertyPath(Height)); 

的InnerException: +的InnerException空的System.Exception

回答

1

試試這個,

Storyboard.SetTargetProperty(animation, new PropertyPath(FrameworkElement.HeightProperty)); 
+0

完美!謝謝! – Erik