我有一个使用某些PART控件的自定义控件:
[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!为什么这不能按我预期的那样工作?
最佳答案
对于可能偶然发现本文的其他人,我遇到了同样的问题,我设法通过将以下内容添加到包含我的自定义控件的项目的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)
)]
我控件的模板位于与控件相同的项目中的Theme / Generic.xaml文件中。
关于wpf - 在自定义控件中未调用OnApplyTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2939787/