在C#中定制控件的动画高度

时间:2014-01-27 14:59:05

标签: c# windows-phone-8 listbox windows-phone controls

我正在开发一个应用程序,我需要为用户点击它时制作的控件的高度设置动画。此控件位于名为“taskListBox”的ListBox中。这是我的代码:

        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 null System.Exception

1 个答案:

答案 0 :(得分:1)

试试这个,

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