C# 应用程序模板上的自定义控件仅有时调用
我有一个包含在WPF自定义控件库中的自定义控件,当我在Visual Studio 2012中运行调试器时,OnApplyTemplate()方法大约每隔一段时间才会被调用一次。更具体地说,我在OnApplyTemplate()方法中设置了一个断点,在我第一次运行调试器时,它从未被调用(并且我放在该方法中的代码没有被执行),但当我选择“重新启动”时,它每次都被调用 我已经看了24个类似性质的问题/答案,都建议确保在静态构造函数中设置DefaultStyleKeyProperty(完成),并且在AssemblyInfo.cs文件中设置ResourceDictionaryLocation.None和ResourceDictionaryLocation.SourceAssembly(完成) 我已经做了几十个自定义控件,没有这个问题。这是基本结构:C# 应用程序模板上的自定义控件仅有时调用,c#,wpf,C#,Wpf,我有一个包含在WPF自定义控件库中的自定义控件,当我在Visual Studio 2012中运行调试器时,OnApplyTemplate()方法大约每隔一段时间才会被调用一次。更具体地说,我在OnApplyTemplate()方法中设置了一个断点,在我第一次运行调试器时,它从未被调用(并且我放在该方法中的代码没有被执行),但当我选择“重新启动”时,它每次都被调用 我已经看了24个类似性质的问题/答案,都建议确保在静态构造函数中设置DefaultStyleKeyProperty(完成),并且在As
[TemplatePart(Name = "PART_FileTree", Type = typeof(CustomTreeView))]
public class ExplorerComboBox: Control, INotifyPropertyChanged {
static ExplorerComboBox () {
DefaultStyleKeyProperty.OverrideMetadata(typeof(blinzExplorerComboBox), new FrameworkPropertyMetadata(typeof(blinzExplorerComboBox)));
}
public override void OnApplyTemplate () {
base.OnApplyTemplate();
_fileTree = (CustomTreeView)Template.FindName("PART_FileTree", this);
if (_fileTree != null) {
_fileTree.SelectedItemChanged += part_fileTree_SelectedItemChanged;
_fileTree.SelectionMode = SelectionModalities.SingleSelectionOnly;
try {
var drives = DriveInfo.GetDrives();
foreach (var drive in drives) {
_fileTree.Items.Add(new FileSystemObjectInfo(drive));
}
} catch {
throw new NotImplementedException();
}
} else {
throw new NotImplementedException();
}
}//OnApplyTemplate
}
奇怪的是,当不调用OnApplyTemplate时,子CustomTreeView仍然会填充文件信息,但事件处理程序“SelectedItemChanged”从未设置。“blinzExplorerComboBox”似乎是它的基本类型。。我不确定用你的控件名“ExploreComboBox”试试,这实际上是一个输入错误。它读的是“ExplorerComboBox”而不是“blinzExplorerComboBox”-谢谢你找到了解决这个问题的方法吗?