gpt4 book ai didi

.net - WPF中的动画宽度到实际宽度?

转载 作者:行者123 更新时间:2023-12-04 13:42:16 24 4
gpt4 key购买 nike

如何将元素的宽度从 0 设置为 WPF 中的实际宽度?

我试过这个:

<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.3" To="{Binding ElementName=MyElement, Path=ActualWidth}" From="0" Storyboard.TargetProperty="Width" Storyboard.TargetName="MyElement" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>

如果我将绑定(bind)更改为硬编码值,例如 100 ,那么宽度是正确的动画,除了我想绑定(bind)到元素的实际宽度。

如果重要, MyElement是一个边框,我正在为一个选项卡项设置动画。

为了记录,这也不起作用:
To="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualWidth}"

最佳答案

我确定这是错误的,原因有很多,请随时告诉我我违反了多少 WPF 法律,但是.. 我通过创建自己的 BindableDoubleAnimation 解决了同样的问题。

public class BindableDoubleAnimation : DoubleAnimationBase
{
DoubleAnimation internalAnimation;

public DoubleAnimation InternalAnimation { get { return internalAnimation; } }

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

/// <summary>
/// Dependency backing property for the <see cref="To"/> property.
/// </summary>
public static readonly DependencyProperty ToProperty =
DependencyProperty.Register("To", typeof(double), typeof(BindableDoubleAnimation), new UIPropertyMetadata(0d, new PropertyChangedCallback((s, e) =>
{
BindableDoubleAnimation sender = (BindableDoubleAnimation)s;
sender.internalAnimation.To = (double)e.NewValue;
})));


public double From
{
get { return (double)GetValue(FromProperty); }
set { SetValue(FromProperty, value); }
}

/// <summary>
/// Dependency backing property for the <see cref="From"/> property.
/// </summary>
public static readonly DependencyProperty FromProperty =
DependencyProperty.Register("From", typeof(double), typeof(BindableDoubleAnimation), new UIPropertyMetadata(0d, new PropertyChangedCallback((s, e) =>
{
BindableDoubleAnimation sender = (BindableDoubleAnimation)s;
sender.internalAnimation.From = (double)e.NewValue;
})));


public BindableDoubleAnimation()
{
internalAnimation = new DoubleAnimation();
}

protected override double GetCurrentValueCore(double defaultOriginValue, double defaultDestinationValue, AnimationClock animationClock)
{
return internalAnimation.GetCurrentValue(defaultOriginValue, defaultDestinationValue, animationClock);
}

protected override Freezable CreateInstanceCore()
{
return internalAnimation.Clone();;
}
}

我现在可以自由地为 To From 属性使用绑定(bind)。
<local:BindableDoubleAnimation Storyboard.TargetProperty="Width" From="0" To="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=Window}}"/>

关于.net - WPF中的动画宽度到实际宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10994716/

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