【问题标题】:WPF PropertyPath on Custom control自定义控件上的 WPF PropertyPath
【发布时间】:2017-07-18 19:40:27
【问题描述】:

我有一个问题,我创建了一个名为 Tile 的控件(就像 Windows 10 上的 Tile)。 此图块使用投影类模拟 3D 旋转,类似于 Silverlight 中的投影类。

我们有一个像这样的基础投影类:

abstract public class Projection
    : FrameworkElement
{
    static public readonly DependencyProperty RotationZProperty = DependencyProperty.Register(
          nameof(RotationZ),
          typeof(double),
          typeof(Projection),
          new UIPropertyMetadata(0.0, OnRotationChanged));

    static public readonly DependencyProperty RotationYProperty = DependencyProperty.Register(
            nameof(RotationY),
            typeof(double),
            typeof(Projection),
            new UIPropertyMetadata(0.0, OnRotationChanged));

    static public readonly DependencyProperty RotationXProperty = DependencyProperty.Register(
            nameof(RotationX),
            typeof(double),
            typeof(Projection),
            new UIPropertyMetadata(0.0, OnRotationChanged));

public double RotationZ
{
    get { return this.GetValue<double>(RotationZProperty); }
    set { SetValue(RotationZProperty, value); }
}

public double RotationY
{
    get { return this.GetValue<double>(RotationYProperty); }
    set { SetValue(RotationYProperty, value); }
}

public double RotationX
{
    get { return this.GetValue<double>(RotationXProperty); }
    set { SetValue(RotationXProperty, value); }
}

public FrameworkElement Child
{
    get;
    set;
}

static private void OnRotationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is Projection pl)
            {
                pl.OnRotationChanged();
            }
        }

        private void OnRotationChanged()
        {
            // Some code
        }}

After that, we have the PlaneProjectionClass :

     [ContentProperty("Child")]
    sealed public class PlaneProjection
        : Projection
    {   
    }

Tile 类使用 Projection 类型的依赖属性:

     public class Tile
    {
     static public readonly DependencyProperty PlaneProjectionProperty = DependencyProperty.Register(
            nameof(Projection),
            typeof(Projection),
            typeof(Tile),
            new UIPropertyMetadata());

 public Projection Projection
        {
            get { return (Projection)GetValue(PlaneProjectionProperty); }
            private set { SetValue(PlaneProjectionProperty, value); }
        }

        override public void OnApplyTemplate()
        {
            Projection = GetTemplateChild("PART_Projection") as Projection;
        }
     }

所以对于 XAML,我们在 ControlTemplate 中有这个:

    <controls:PlaneProjection x:Name="PART_PlaneProjection">
    <Border>
    <Grid>
      <!-- Some design -->
    </Grid>
    </Border>
</controls:PlaneProjection>

现在我想为平面投影设置动画。

所以我创建了故事板并使用 rotationX 为投影设置动画:

static public void CreateAnimation(Tile tile)
{
    Storyboard.SetTarget(anim, tile);
    Storyboard.SetTargetProperty(doubleAnim, new PropertyPath("(Tile.Projection).(PlaneProjection.RotationX"));
 }

但在调试时,我有这个错误:无法解析属性路径上属性的所有引用'(Tile.Projection).(PlaneProjection.RotationX)

我不明白这个错误:(关于在自定义控件上使用 PropertyPath 有什么想法吗?

【问题讨论】:

    标签: wpf animation propertypath


    【解决方案1】:

    类 Tile 中的 Projection 属性不遵循依赖属性的命名约定。

    它应该例如看起来像这样:

    public static readonly DependencyProperty PlaneProjectionProperty =
        DependencyProperty.Register(nameof(PlaneProjection), typeof(Projection), typeof(Tile));
    
    public Projection PlaneProjection
    {
        get { return (Projection)GetValue(PlaneProjectionProperty); }
        private set { SetValue(PlaneProjectionProperty, value); }
    }
    

    属性路径应该是这样的:

    Storyboard.SetTargetProperty(anim, new PropertyPath("PlaneProjection.RotationX"));
    

    您甚至不需要故事板。只需拨打电话

    tile.PlaneProjection.BeginAnimation(Projection.RotationXProperty, anim);
    

    请注意,私有设置器不会将依赖项属性设为只读。详情请见Read-Only Dependency Properties。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 2015-08-28
      相关资源
      最近更新 更多