gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:12:47 24 4
gpt4 key购买 nike

在我的 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);

堆栈跟踪

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*"/>

关于c# - 无法设置对象的属性,因为它处于只读状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27736537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com