在我的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>
gla
是GridLengthAnimationObject
。尝试设置
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
中,我有许多Buttons
。 LeftGrid
的默认宽度为7*
,而RightGrid
最初设置为0*
(不可见)。在Button
中单击LeftGrid
时,RightGrid
应扩展为3*
的宽度。 RightGrid
的此扩展应设置为动画。最后,如果展开RightGrid
并连续两次单击LeftGrid
中的按钮,则RightGrid
应收缩回0*
。 最佳答案
GridLengthAnimation
的最简单实现如下所示。它仅添加To
属性(例如DoubleAnimation具有),但不添加From
或By
属性。因此,它只能将属性从其当前值动画化为指定的目标值。
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*"/>
关于c# - 无法设置对象的属性,因为它处于只读状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27736537/