2010-05-30 54 views
11

叫我有一個使用某些部分控制自定義控件:OnApplyTemplate不自定義控件

[TemplatePart(Name = "PART_TitleTextBox", Type = typeof(TextBox))] 
    [TemplatePart(Name = "PART_TitleIndexText", Type = typeof(Label))] 
    [TemplatePart(Name = "PART_TimeCodeInText", Type = typeof(TextBlock))] 
    [TemplatePart(Name = "PART_TimeCodeOutText", Type = typeof(TextBlock))] 
    [TemplatePart(Name = "PART_ApprovedImage", Type = typeof(Image))] 
    [TemplatePart(Name = "PART_CommentsImage", Type = typeof(Image))] 
    [TemplatePart(Name = "PART_BookmarkedImage", Type = typeof(Image))] 
    public class TitleBoxNew : Control 
    { 
     static TitleBoxNew() 
     { 
      DefaultStyleKeyProperty.OverrideMetadata(
       typeof(TitleBoxNew), 
       new FrameworkPropertyMetadata(typeof(TitleBoxNew))); 
     } 

     public TitleBoxNew() { } 

     // ... rest of class 
    } 

這種控制是覆蓋OnApplyTemplate:

public override void OnApplyTemplate() 
{ 
     base.OnApplyTemplate(); 

     InitializeEvents(); 
} 

效果很好,大部分時間。我在窗口中的自定義選項卡控件中添加了控件,並且不知何故,OnApplyTemplate從未被調用過該控件!爲什麼不按照我的預期工作?

+4

-1發脾氣。 – zneak 2010-05-30 18:26:53

+6

你是第一次成員,你使用的是F字。在發佈之前試着看看這個社區是否喜歡它。 – vladv 2010-05-30 18:35:32

+2

-1語言。 – 2010-05-30 18:37:01

回答

3

我看不到你的構造,但不要忘記設置DefaultStyleKey:

DefaultStyleKeyProperty.OverrideMetadata(typeof(TitleBoxNew), new FrameworkPropertyMetadata(typeof(TitleBoxNew))); 
+0

是我frogot一提的是,我已經得到了這一點: 靜態TitleBoxNew(){ DefaultStyleKeyProperty.OverrideMetadata(typeof運算(TitleBoxNew),新FrameworkPropertyMetadata(typeof運算(TitleBoxNew))); } public TitleBoxNew() { } – 2010-05-30 19:01:47

+0

不錯的包裝計算器,做得好 – 2010-05-30 19:02:14

+10

請編輯您的orignal文章以包含您的構造函數;評論不是正確的代碼。 – 2010-05-30 19:24:18

37

對於任何人在這個帖子誰可能絆倒,我有同樣的問題,我設法解決它通過添加以下內容包含我的自定義控件項目的AssemblyInfo.cs:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 
    //(used if a resource is not found in the page, 
    // or application resource dictionaries) 
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries) 
)] 

我的控件模板位於文件主題/ Generic.xaml在同一個項目的控制。

+2

這節省了我的一天。從字面上看。非常感謝。 – 2012-11-13 10:22:46

+1

在我們將一些WPF自定義控件移動到最初未創建爲「WPF自定義控件庫」的常規C#類庫之後,這解決了我的問題。順便說一句:還將所需的 XML元素添加到.csproj以將類庫升級到「真實」的WPF類庫。 – candritzky 2014-09-30 06:41:34

20

另外兩個答案是正確的......但不完整。根據this post(以及我剛剛解決這個問題的經驗),你需要檢查4件事:(出於某種原因,如果我使用數字或破折號,這篇文章中的代碼塊不會保持格式化...所以它是)

答:控件模板和樣式應位於Generic.xaml文件中,該文件位於項目根目錄Themes

B.確保您的命名空間是正確的Generic.xaml

C.設置在控制的構造風格的關鍵。也廣泛推薦你將以下內容放入靜態構造函數中。

static YourControl() 
{ 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(YourControl), new FrameworkPropertyMetadata(typeof(YourControl))); 
} 

D.請確保以下是你的AssemblyInfo.cs

[assembly: ThemeInfo(ResourceDictionaryLocation.None, 
//where theme specific resource dictionaries are located 
//(used if a resource is not found in the  
// or application resource dictionaries) 
ResourceDictionaryLocation.SourceAssembly 
//where the generic resource dictionary is located 
//(used if a resource is not found in the page, 
// app, or any theme specific resource dictionaries) 
)] 
+0

步驟'A'足以解決我的問題,謝謝! ('uwp') – rubStackOverflow 2017-01-08 18:37:23

1

我要去,因爲這兩個以上的答案是不完整的,因爲我一直在這個問題對一些奮力加入我自己的答案是時候了。

正如所說到MoMo和開G的上方:

A.控件模板和樣式應設在 Generic.xaml文件名爲您的項目的根的主題元素文件夾。

B.確保您的命名空間是正確的Generic.xaml

C.設置在控制的構造風格的關鍵。

D.確保主題屬性AssemblyInfo.cs文件中

,但你還需要確保你的Generic.xaml文件設置爲生成操作爲頁面:不要複製。

如果您未能執行此操作或將值設置爲除此以外的值,則OnApplyTemplate()方法將不會被調用。

+0

D部分特別重要,IMO。 – 2015-07-08 16:40:41

1

回答@MoMo是正確的,但另外:

E:據預計,該主題/ Generic.xaml是在項目的根目錄下。如果情況並非如此,並且您的Generic.xaml不在根目錄下,那麼您必須在根目錄中創建一個包含Generic.xaml的主題(Generic.xaml只是ResourceDictionary類型)。在那個Generic.xaml中,你需要引用Generic.xaml的位置。

例如爲:

<ResourceDictionary Source="/Foo.Bar;component/Controls/FooControl/Themes/Generic.xaml" />