动画宽度为WPF中的实际宽度? [英] Animation width to actual width in WPF?
问题描述
我如何动画元素的宽度从0到WPF的实际宽度?
我尝试这样做:
< ControlTemplate.Triggers>
< EventTrigger中RoutedEvent =装>
< BeginStoryboard>
<情节板>
< DoubleAnimation是持续时间=0:0:0.3为={结合的ElementName = MyElement,路径= ActualWidth的}从=0Storyboard.TargetProperty =宽度Storyboard.TargetName =MyElement/>
< /故事板>
< / BeginStoryboard>
< / EventTrigger中>
< /ControlTemplate.Triggers>
如果我更改绑定到一个硬盘codeD值,例如 100
,那么宽度适当的动画,但我想绑定到实际宽度元素。
如果它的事项, MyElement
是一个边界,而我动画选项卡项目。
有关的记录,这也不行:
来={绑定的RelativeSource = {的RelativeSource AncestorType = {X:边框类型}},路径= ActualWidth的}
我敢肯定,这是错误的,这么多的理由,并请随时告诉我有多少WPF的法律,我违反了,但..我解决了同样的通过创建自己的BindableDoubleAnimation的问题。
公共类BindableDoubleAnimation:DoubleAnimationBase
{
DoubleAnimation是internalAnimation;
公共DoubleAnimation是InternalAnimation {{返回internalAnimation; }}
公共一倍,达到
{
{返回(双)的GetValue(ToProperty); }
集合{的SetValue(ToProperty,价值); }
}
///<总结>
///依赖支持属性的<看到CREF =要/>属性。
///< /总结>
公共静态只读的DependencyProperty ToProperty =
DependencyProperty.Register(要的typeof(双),typeof运算(BindableDoubleAnimation),新UIPropertyMetadata(0D,新PropertyChangedCallback((S,E)=>
{
BindableDoubleAnimation发送=(BindableDoubleAnimation)S;
sender.internalAnimation.To =(双)e.NewValue;
})));
公共双人房
{
{返回(双)的GetValue(FromProperty); }
集合{的SetValue(FromProperty,价值); }
}
///<总结>
///依赖支持属性的<看到CREF =从/>属性。
///< /总结>
公共静态只读的DependencyProperty FromProperty =
DependencyProperty.Register(从的typeof(双),typeof运算(BindableDoubleAnimation),新UIPropertyMetadata(0D,新PropertyChangedCallback((S,E)=>
{
BindableDoubleAnimation发送=(BindableDoubleAnimation)S;
sender.internalAnimation.From =(双)e.NewValue;
})));
公共BindableDoubleAnimation()
{
internalAnimation =新DoubleAnimation是();
}
保护覆盖双GetCurrentValueCore(双defaultOriginValue,双defaultDestinationValue,AnimationClock应用AnimationClock应用)
{
返回internalAnimation.GetCurrentValue(defaultOriginValue,defaultDestinationValue,AnimationClock应用);
}
保护覆盖可冻结CreateInstanceCore()
{
返回internalAnimation.Clone();;
}
}
我现在很自由使用绑定收件人从属性。
<地方:BindableDoubleAnimation Storyboard.TargetProperty =宽度从=0={结合ActualWidth的,的RelativeSource = {的RelativeSource AncestorType =窗口}}/>
How can I animate the width of an element from 0 to its actual width in WPF?
I tried this:
<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>
If I change the binding to a hard coded value, like 100
, then the width is properly animated, except that I want to bind to the actual width of the element.
If it matters, MyElement
is a border, and I'm animating a tab item.
For the record, this does not work either:
To="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}}, Path=ActualWidth}"
I'm sure this is wrong for SO many reasons and please feel free to tell me how many WPF laws I've violated but.. I solved the same problem by creating my own 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();;
}
}
I'm now free to use bindings for the To From properties.
<local:BindableDoubleAnimation Storyboard.TargetProperty="Width" From="0" To="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=Window}}"/>
这篇关于动画宽度为WPF中的实际宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!