wpf - vb - 动画网格分析器/停靠面板

时间:2012-10-23 10:09:05

标签: wpf

这与我之前的问题有关 - 我想为网格分割器设置动画(使面板滑入/滑出视图)。我们非常擅长VB并且已经有一个VB项目,所以如果可以的话,我们希望继续使用VB,但是大多数WPF示例似乎都在XAML或CS中。

我有一些简单的VB动画代码可以工作,但是:

当然,需要设置动画的是网格列/行的宽度/高度,这不是依赖属性。我在CS中发现了一些聪明的东西来创建一个依赖属性但是无法将其转换为vb。所以我找到了一个简单的解决方法,即在网格单元格中设置dockpanel的动画,捕获它的大小更改事件,并使用这些来设置单元格网格大小。它有效,但我想知道它是否效率低,因为有两件事分别改变了?此外,我必须(当动画完成时)将网格单元格大小设置为正确比例的*,并将dockpanel大小设置为自动。

它有效,但看起来有点笨拙 - 有人有一个例子,可以直接从VB中为网格制作动画吗?还有其他任何建议吗?

由于

1 个答案:

答案 0 :(得分:0)

作为参考,这里是用于为gridsplitter设置动画的依赖项属性的VB代码:

Public Class GridLengthAnimation
Inherits AnimationTimeline
Public Sub New()
End Sub

Public Property From() As GridLength
    Get
        Return DirectCast(GetValue(FromProperty), GridLength)
    End Get
    Set(value As GridLength)
        SetValue(FromProperty, value)
    End Set
End Property

Public Shared ReadOnly FromProperty As DependencyProperty 
  = DependencyProperty.Register("From", GetType(GridLength), 
      GetType(GridLengthAnimation))

Public Property [To]() As GridLength
    Get
        Return DirectCast(GetValue(ToProperty), GridLength)
    End Get
    Set(value As GridLength)
        SetValue(ToProperty, value)
    End Set
End Property

Public Shared ReadOnly ToProperty As DependencyProperty 
   = DependencyProperty.Register("To", GetType(GridLength), 
        GetType(GridLengthAnimation))

Public Overrides ReadOnly Property TargetPropertyType() As Type
    Get
        Return GetType(GridLength)
    End Get
End Property

Protected Overrides Function CreateInstanceCore() As Freezable
    Return New GridLengthAnimation()
End Function

Public Overrides Function GetCurrentValue
      (defaultOriginValue As Object, 
       defaultDestinationValue As Object, 
       animationClock As AnimationClock) As Object
    Dim fromValue As Double = Me.From.Value
    Dim toValue As Double = Me.[To].Value

    If fromValue > toValue Then
        Return New GridLength((1 - animationClock.CurrentProgress.Value) 
             * (fromValue - toValue) + toValue, 
                If(Me.[To].IsStar, GridUnitType.Star, GridUnitType.Pixel))
    Else
          Return New GridLength((animationClock.CurrentProgress.Value) * 
                (toValue - fromValue) + fromValue, 
                   If(Me.[To].IsStar, GridUnitType.Star, GridUnitType.Pixel))
    End If
End Function
End Class