2013-10-31 43 views
0

我的用戶控件有以下DP:如何通過樣式設置器設置DepedencyProperty?

public static readonly DependencyProperty ButtonAnimationColorProperty = 
     DependencyProperty.Register("ButtonAnimationColor", typeof(Color), typeof(MyControl), 
     new FrameworkPropertyMetadata(Colors.RoyalBlue, FrameworkPropertyMetadataOptions.AffectsRender, ThemeUpdate)); 

    public Color ButtonAnimationColor 
    { 
     get { return (Color)GetValue(ButtonAnimationColorProperty); } 
     set { SetValue(ButtonAnimationColorProperty , value); } 
    } 

這種控制被編譯成一個DLL,我在其他解決方案中使用。它完美的作品很好,當我直接設置:

<ns:MyControl> 
    <ns:MyControl.Style> 
     <Style> 
      <Setter Property="ButtonAnimationColor" Value="Green" /> 
     </Style> 
    </ns:MyControl.Style> 
</ns:MyControl> 

它給我以下錯誤:

<ns:MyControl ButtonAnimationColor="Green" /> 

,當我試圖通過使用樣式二傳手設置這個DP一樣,發生的問題

成員「ButtoAnimationColor」無法識別或不可訪問。

我需要在代碼中進行哪些更改才能設置屬性?

+0

拼寫是否正確呢?你的錯誤是'ButtonnimationColor'。 – gleng

+0

對不起,考慮一下代碼。我會更新這個問題。 – Guilherme

回答

2

嘗試設置目標類型的樣式:

<ns:MyControl.Style> 
    <Style TargetType="{x:Type ns:MyControl}"> 
     <Setter Property="ButtonAnimationColor" Value="Green" /> 
    </Style> 
</ns:MyControl.Style> 
+0

完美地工作,只需要像這樣的引號:TargetType =「{x:Type ns:MyControl}」 – Guilherme

+0

Woops。將它們添加到答案中。 – DamenEU

相關問題