2017-07-18 145 views
2

我有一個問題,我創建了一個名爲Tile的控件(比如Windows 10上的Tile)。 此貼片通過使用投影類來模擬旋轉3D,就像Silverlight中的投影類一樣。WPF PropertyPath上的自定義控件

我們有一個底部突起類是這樣的:

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 
    { 
    } 

磁磚類中使用投影類型依賴屬性:

 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,我們有這樣的控件模板:

<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任何想法?

回答

2

類平鋪Projection屬性不遵循依賴公關的命名約定operties。

它應該例如是這樣的:

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