wpf - 在自定义控件中未调用OnApplyTemplate

标签 wpf

我有一个使用某些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/

相关文章:

c# - 属性初始化和比较时间

c# - 将哈希表绑定(bind)到 WPF 组合框

c# - 滚动时弹出窗口不关闭

.net - 自定义 WPF DatePickerTextBox 模板

wpf - 在 WPF 中处理对齐设备像素的任何提示

c# - 在 WPF DataGrid 中保留用户定义的排序顺序

c# - WPF 应用程序中的异常记录,MVVMLight SimpleIoC 隐藏异常而不是抛出它们

c# - 调用弹出窗口的 `.ShowDialog()` 时 WPF 应用程序关闭

c# - 无需框架即可相互通信的 ViewModel

c# - POS 应用程序开发 - 收据打印