无法设置对象的属性,因为它处于只读状态

Cannot set a property on object because it is in a read-only state

在我的 XAML 中,我有以下内容:

<DataTemplate x:Key="ItemTemplate">
        <DockPanel Width="Auto">
            <Button Click="SelectMovie_Click" DockPanel.Dock="Top">
                <Button.Template>
                    <ControlTemplate >
                        <Image Source="{Binding image}"/>
                    </ControlTemplate>
                </Button.Template>                   
                <Button.Triggers>
                    <EventTrigger RoutedEvent="Button.Click">
                        <BeginStoryboard>
                            <Storyboard>
                                <local:GridLengthAnimation
                                    Storyboard.Target="{Binding ElementName=col2}"
                                    Storyboard.TargetProperty="Width"
                                    Duration="0:0:2"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Button.Triggers>
            </Button>
            <TextBlock Text="{Binding title}" HorizontalAlignment="Center" DockPanel.Dock="Bottom"/>
        </DockPanel>
    </DataTemplate>

<Grid Grid.Row="2" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Name="col1"  Width="{Binding ElementName=root, Path=DataContext.gla.LeftGridWidth}"/>
            <ColumnDefinition Name="col2" Width="{Binding ElementName=root, Path=DataContext.gla.RightGridWidth}"/>
        </Grid.ColumnDefinitions>
         ...
         ...
</Grid>

glaGridLengthAnimationObject.

当我尝试设置 Dependency Property

时出现上述错误
public class GridLengthAnimation : AnimationTimeline
{
    public override Type TargetPropertyType
    {
        get
        {
            return typeof(GridLength);
        }
    }

    protected override System.Windows.Freezable CreateInstanceCore()
    {
        return new GridLengthAnimation();
    }

    public GridLengthAnimation()
    {
        LeftGridWidth = new GridLength(7, GridUnitType.Star);
        RightGridWidth = new GridLength(0, GridUnitType.Star);
    }

    public static readonly DependencyProperty LeftGridWidthProperty = DependencyProperty.Register("LeftGridWidth", typeof(GridLength), typeof(GridLengthAnimation));
    public GridLength LeftGridWidth
    {
        get { return (GridLength)this.GetValue(LeftGridWidthProperty); }
        set { this.SetValue(LeftGridWidthProperty, value); }
    }

    public static readonly DependencyProperty RightGridWidthProperty = DependencyProperty.Register("RightGridWidth", typeof(GridLength), typeof(GridLengthAnimation));
    public GridLength RightGridWidth
    {

        get { return (GridLength)this.GetValue(RightGridWidthProperty); }
        set { this.SetValue(RightGridWidthProperty, value); }
    }

    public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock)
    {
        double rightGridVal = ((GridLength)GetValue(GridLengthAnimation.RightGridWidthProperty)).Value;
        double leftGridVal = ((GridLength)GetValue(GridLengthAnimation.LeftGridWidthProperty)).Value;

        RightGridWidth = rightGridVal == 0 ? new GridLength(3, GridUnitType.Star) : new GridLength(0, GridUnitType.Star);

        return RightGridWidth;
    }        
}

此处出现错误:

 RightGridWidth = rightGridVal == 0 ? new GridLength(3, GridUnitType.Star) : new GridLength(0, GridUnitType.Star);

Stack Trace

System.InvalidOperationException: Cannot set a property on object   'VideoManager.GridLengthAnimation' because it is in a read-only state.
at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value,   PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue,   OperationType operationType, Boolean isInternal)
at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
at VideoManager.GridLengthAnimation.set_RightGridWidth(GridLength value) in    c:\Users\Giri\Documents\Visual Studio   2013\Projects\VideoManager\VideoManager\GridLengthAnimation.cs:line 47
at VideoManager.GridLengthAnimation.GetCurrentValue(Object defaultOriginValue, Object    defaultDestinationValue, AnimationClock animationClock) in c:\Users\Giri\Documents\Visual Studio   2013\Projects\VideoManager\VideoManager\GridLengthAnimation.cs:line 56
A first chance exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll     

在我的 LeftGrid 中,我有多个 ButtonsLeftGrid 的默认宽度为 7*,而 RightGrid 最初设置为 0*(不可见)。当在 LeftGrid 中单击 Button 时,RightGrid 应该扩展到 3* 的宽度。 RightGrid 的这个扩展应该是动画的。最后,如果 RightGrid 被展开并且 LeftGrid 中的按钮被连续点击两次,RightGrid 应该收缩回 0*

GridLengthAnimation 的最简单实现如下所示。它只添加了 To 属性(例如 DoubleAnimation 有),但没有 FromBy 属性。因此,它只能将 属性 从其当前值动画化为指定的目标值。

public class GridLengthAnimation : AnimationTimeline
{
    public static readonly DependencyProperty ToProperty =
        DependencyProperty.Register(
            "To", typeof(GridLength), typeof(GridLengthAnimation));

    public GridLength To
    {
        get { return (GridLength)GetValue(ToProperty); }
        set { SetValue(ToProperty, value); }
    }

    public override Type TargetPropertyType
    {
        get { return typeof(GridLength); }
    }

    protected override Freezable CreateInstanceCore()
    {
        return new GridLengthAnimation();
    }

    public override object GetCurrentValue(
        object defaultOriginValue, object defaultDestinationValue,
        AnimationClock animationClock)
    {
        var from = (GridLength)defaultOriginValue;

        if (from.GridUnitType != To.GridUnitType ||
            !animationClock.CurrentProgress.HasValue)
        {
            return from;
        }

        var p = animationClock.CurrentProgress.Value;

        return new GridLength(
            (1d - p) * from.Value + p * To.Value,
            from.GridUnitType);
    }
}

你会像这样使用它:

<local:GridLengthAnimation
    Storyboard.Target="{Binding ElementName=col2}"
    Storyboard.TargetProperty="Width"
    Duration="0:0:2" To="3*"/>