如您所知,我们无法直接在 ResourceDictionary xaml 中针对内部或私有类重新定义任何样式,但我们可以从后面的代码中完成。
所以,我们只需要通过反射找到类型,并使用我们默认的 ContextMenu 和 MenuItem 样式创建新样式。
private void Initialize()
{
var presentationFrameworkAssembly = typeof(Application).Assembly;
var contextMenuStyle = FindResource(typeof(ContextMenu)) as Style;
var editorContextMenuType = Type.GetType("System.Windows.Documents.TextEditorContextMenu+EditorContextMenu, " + presentationFrameworkAssembly);
if (editorContextMenuType != null)
{
var editorContextMenuStyle = new Style(editorContextMenuType, contextMenuStyle);
Application.Current.Resources.Add(editorContextMenuType, editorContextMenuStyle);
}
var menuItemStyle = Application.Current.FindResource(typeof(MenuItem)) as Style;
var editorMenuItemType = Type.GetType("System.Windows.Documents.TextEditorContextMenu+EditorMenuItem, " + presentationFrameworkAssembly);
if (editorMenuItemType != null)
{
var editorContextMenuStyle = new Style(editorMenuItemType, menuItemStyle);
Application.Current.Resources.Add(editorMenuItemType, editorContextMenuStyle);
}
}
此外,我们可以生成自定义 ResourceDictionary 以重新定义默认隐藏样式,并生成一个虚拟 DefaultHiddenStyle.xaml 以允许将其像其他人一样包含在 MergedDictionaries 中。
<local:DefaultHiddenStyleResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Styles">
<!-- No entries are required -->
</local:DefaultHiddenStyleResourceDictionary>
namespace Styles
{
public class DefaultHiddenStyleResourceDictionary : ResourceDictionary
{
public DefaultHiddenStyleResourceDictionary()
{
// Run OnResourceDictionaryLoaded asynchronously to ensure other ResourceDictionary are already loaded before adding new entries
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(OnResourceDictionaryLoaded));
}
private void OnResourceDictionaryLoaded()
{
var presentationFrameworkAssembly = typeof(Application).Assembly;
AddEditorContextMenuDefaultStyle(presentationFrameworkAssembly);
AddEditorMenuItemDefaultStyle(presentationFrameworkAssembly);
}
private void AddEditorContextMenuDefaultStyle()
{
var presentationFrameworkAssembly = typeof(Application).Assembly;
var contextMenuStyle = Application.Current.FindResource(typeof(ContextMenu)) as Style;
var editorContextMenuType = Type.GetType("System.Windows.Documents.TextEditorContextMenu+EditorContextMenu, " + presentationFrameworkAssembly);
if (editorContextMenuType != null)
{
var editorContextMenuStyle = new Style(editorContextMenuType, contextMenuStyle);
Add(editorContextMenuType, editorContextMenuStyle);
}
}
private void AddEditorMenuItemDefaultStyle(Assembly presentationFrameworkAssembly)
{
var menuItemStyle = Application.Current.FindResource(typeof(MenuItem)) as Style;
var editorMenuItemType = Type.GetType("System.Windows.Documents.TextEditorContextMenu+EditorMenuItem, " + presentationFrameworkAssembly);
if (editorMenuItemType != null)
{
var editorContextMenuStyle = new Style(editorMenuItemType, menuItemStyle);
Add(editorMenuItemType, editorContextMenuStyle);
}
}
}
}