2011-09-18 50 views
2

我創建了用戶控件MyUserControl。現在我想要創建自定義控件MyCustomControl,它源自MyUserControl。 MyCustomControl.cs代碼如下:ArgumentException在從用戶控件派生的自定義控件中設置DefaultStyleKey - Silverlight 4

public class MyCustomControl : MyUserControl 
    { 
     public MyCustomControl() 
     { 
      this.DefaultStyleKey = typeof(MyCustomControl); 
     } 
    } 

我有主題/ Generic.xaml的風格

<Style TargetType="local:MyCustomControl"> 
... 
</Style> 

實例化MyCustomControl在運行時,我得到的ArgumentException執行一行的文件

this.DefaultStyleKey = typeof(MyCustomControl); 

我錯過了什麼?

回答

2

分配是派生從UserControlDefaultStyleKey一個類型是明確拋出一個ArgumentException(爲什麼一個ArgumentException,爲什麼沒有explanitory消息只包括SL的團隊知道)不允許的。

A UserControl不能模板化接收它自己關聯的Xaml。那就是UserControl的整點。如果你想以你嘗試的方式繼承它,你需要將MyUserControl轉換爲模板控件。

+0

我明白了。謝謝!是否有可能創建CustomControl1與它的默認樣式,然後創建CustomControl2派生自CustomControl1與CustomControl2也有它自己的默認樣式?最初我嘗試了這種情況,但System.Exception在運行時發生 – EvAlex

相關問題