在silverlight中设置依赖属性的动画

时间:2010-03-12 16:23:01

标签: silverlight

我正在尝试动画一个依赖属性的例子,以便了解它是如何工作的。 所有它应该做的是改变列的宽度,但由于某种原因,我的断点没有被击中,显然没有任何事情发生。

代码是:

<UserControl x:Class="SilverlightApplication1.MainPage"
    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" d:DesignWidth="640" d:DesignHeight="480" x:Name="Myself">
    <UserControl.Resources>
        <Storyboard x:Name="Storyboard1">
            <DoubleAnimation  Storyboard.TargetName="Myself" Storyboard.TargetProperty="ColumnWidth" To="0" Duration="00:00:05"/>
        </Storyboard>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Grid  Background="Black">
            <Grid.RenderTransform>
                <TransformGroup>
                    <ScaleTransform />
                    <SkewTransform />
                    <RotateTransform />
                    <TranslateTransform />
                </TransformGroup>
            </Grid.RenderTransform>
            <Grid.ColumnDefinitions>
                <ColumnDefinition x:Name="Column1"
                                  Width="140" />
                <ColumnDefinition Width="250" />
                <ColumnDefinition Width="250" />
            </Grid.ColumnDefinitions>
            <Rectangle Fill="White" Stroke="Black" Margin="5"/>
            <Rectangle Fill="White" Stroke="Black" Margin="5" Grid.Column="1"/>
            <Rectangle Fill="White" Stroke="Black" Margin="5" Grid.Column="2"/>
        </Grid>

       <StackPanel Grid.Row="1">
               <Button x:Name="button1" Content="Change column width"
                Click="button1_Click" Height="100"/>
       </StackPanel>

    </Grid>

</UserControl>

以及后面的代码:

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Storyboard1.Begin();
        }

        public static readonly DependencyProperty ColumnWidthProperty = DependencyProperty.Register("ColumnWidth",
                                                                                                    typeof (double),
                                                                                                    typeof (MainPage),
                                                                                                    new PropertyMetadata
                                                                                                      (ColumnWidthChanged));
        public double ColumnWidth
        {
            get
            {
                return (double)GetValue(ColumnWidthProperty);
            }
            set
            {
                SetValue(ColumnWidthProperty, value);
            }
        }

        private static void ColumnWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var mainPage = (MainPage) d;
            mainPage.Column1.Width = new GridLength((double)e.NewValue);
        }
    }
}

JD。

1 个答案:

答案 0 :(得分:1)

您为0到0的值设置了动画,这就是为什么没有调用ColumnWidthChanged方法,因为它没有更改。