OnApplyTemplate not called in Custom Control

前端 未结 6 2055
忘掉有多难
忘掉有多难 2021-02-01 21:47

I have a Custom Control which uses some PART controls:

 [TemplatePart(Name = \"PART_TitleTextBox\", Type = typeof(TextBox))]
    [TemplatePart(Name = \"PART_Titl         


        
6条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 22:38

    I am going to add my own answer because both the above answers are incomplete as I have been struggling with this problem for some time now.

    As said by MoMo and Kai G above:

    A. The controls template and styles should be located in the Generic.xaml file a folder called Themes of the root of your project.

    B. Make sure your namespaces are correct in the Generic.xaml

    C. Set the style key in the constructor of your control.

    D. Ensure the theme attribute is in the assemblyinfo.cs file

    BUT you also need to ensure that your Generic.xaml file is set for the Build Action as Page:Do Not copy.

    If you fail to do this or the value was somehow set to something other than this, the OnApplyTemplate() method will not be invoked.

提交回复
热议问题