优化更新代码,添加界面功能并整合
This commit is contained in:
69
WPFluent/Controls/ContextMenu/ContextMenu.xaml
Normal file
69
WPFluent/Controls/ContextMenu/ContextMenu.xaml
Normal file
@@ -0,0 +1,69 @@
|
||||
<!--
|
||||
This Source Code Form is subject to the terms of the MIT License.
|
||||
If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
|
||||
Copyright (C) Leszek Pomianowski and WPF UI Contributors.
|
||||
All Rights Reserved.
|
||||
-->
|
||||
|
||||
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
|
||||
<Style x:Key="UiContextMenu" TargetType="{x:Type ContextMenu}">
|
||||
<Setter Property="TextElement.Foreground" Value="{DynamicResource ContextMenuForeground}" />
|
||||
<Setter Property="Foreground" Value="{DynamicResource ContextMenuForeground}" />
|
||||
<Setter Property="Background" Value="{DynamicResource ContextMenuBackground}" />
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource ContextMenuBorderBrush}" />
|
||||
<Setter Property="MinWidth" Value="140" />
|
||||
<Setter Property="Padding" Value="0" />
|
||||
<Setter Property="Margin" Value="0" />
|
||||
<Setter Property="HasDropShadow" Value="False" />
|
||||
<Setter Property="Grid.IsSharedSizeScope" Value="True" />
|
||||
<Setter Property="Popup.PopupAnimation" Value="None" />
|
||||
<Setter Property="SnapsToDevicePixels" Value="True" />
|
||||
<Setter Property="OverridesDefaultStyle" Value="True" />
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="{x:Type ContextMenu}">
|
||||
<Border
|
||||
x:Name="Border"
|
||||
Padding="0,3,0,3"
|
||||
Background="{TemplateBinding Background}"
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="1"
|
||||
CornerRadius="8">
|
||||
<Border.RenderTransform>
|
||||
<TranslateTransform />
|
||||
</Border.RenderTransform>
|
||||
<StackPanel
|
||||
ClipToBounds="True"
|
||||
IsItemsHost="True"
|
||||
KeyboardNavigation.DirectionalNavigation="Cycle"
|
||||
Orientation="Vertical" />
|
||||
</Border>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="IsOpen" Value="True">
|
||||
<Trigger.EnterActions>
|
||||
<BeginStoryboard>
|
||||
<Storyboard>
|
||||
<DoubleAnimation
|
||||
Storyboard.TargetName="Border"
|
||||
Storyboard.TargetProperty="(Border.RenderTransform).(TranslateTransform.Y)"
|
||||
From="-90"
|
||||
To="0"
|
||||
Duration="00:00:00.167">
|
||||
<DoubleAnimation.EasingFunction>
|
||||
<CircleEase EasingMode="EaseOut" />
|
||||
</DoubleAnimation.EasingFunction>
|
||||
</DoubleAnimation>
|
||||
</Storyboard>
|
||||
</BeginStoryboard>
|
||||
</Trigger.EnterActions>
|
||||
</Trigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
|
||||
<Style BasedOn="{StaticResource UiContextMenu}" TargetType="{x:Type ContextMenu}" />
|
||||
|
||||
</ResourceDictionary>
|
||||
11
WPFluent/Controls/ContextMenu/ContextMenuLoader.xaml
Normal file
11
WPFluent/Controls/ContextMenu/ContextMenuLoader.xaml
Normal file
@@ -0,0 +1,11 @@
|
||||
<!--
|
||||
This Source Code Form is subject to the terms of the MIT License.
|
||||
If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
|
||||
Copyright (C) Leszek Pomianowski and WPF UI Contributors.
|
||||
All Rights Reserved.
|
||||
-->
|
||||
|
||||
<ResourceDictionary
|
||||
x:Class="WPFluent.Controls.ContextMenuLoader"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" />
|
||||
56
WPFluent/Controls/ContextMenu/ContextMenuLoader.xaml.cs
Normal file
56
WPFluent/Controls/ContextMenu/ContextMenuLoader.xaml.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
|
||||
|
||||
|
||||
/* This Code is based on a StackOverflow-Answer: https://stackoverflow.com/a/56736232/9759874 */
|
||||
|
||||
using System.Reflection;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace WPFluent.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Overwrites ContextMenu-Style for some UIElements (like RichTextBox) that don't take the default ContextMenu-Style by
|
||||
/// default. <para>The code inside this CodeBehind-Class forces this ContextMenu-Style on these UIElements through
|
||||
/// Reflection (because it is only accessible through Reflection it is also only possible through CodeBehind and not
|
||||
/// XAML)</para>
|
||||
/// </summary>
|
||||
public partial class ContextMenuLoader : ResourceDictionary
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ContextMenuLoader"/> class and registers editing styles defined in
|
||||
/// "ContextMenu.xaml" with the <see cref="Dispatcher"/>.
|
||||
/// </summary>
|
||||
public ContextMenuLoader()
|
||||
{
|
||||
// Run OnResourceDictionaryLoaded asynchronously to ensure other ResourceDictionary are already loaded before adding new entries
|
||||
_ = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(OnResourceDictionaryLoaded));
|
||||
}
|
||||
|
||||
private void AddEditorContextMenuDefaultStyle(Assembly currentAssembly)
|
||||
{
|
||||
var editorContextMenuType = Type.GetType(
|
||||
$"System.Windows.Documents.TextEditorContextMenu+EditorContextMenu, {currentAssembly}");
|
||||
|
||||
ResourceDictionary resourceDict = new()
|
||||
{
|
||||
Source = new Uri("pack://application:,,,/WPFluent;component/Controls/ContextMenu/ContextMenu.xaml"),
|
||||
};
|
||||
|
||||
Style contextMenuStyle = (Style)resourceDict["UiContextMenu"];
|
||||
|
||||
if (editorContextMenuType is null || contextMenuStyle is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var editorContextMenuStyle = new Style(editorContextMenuType, contextMenuStyle);
|
||||
Add(editorContextMenuType, editorContextMenuStyle);
|
||||
}
|
||||
|
||||
private void OnResourceDictionaryLoaded()
|
||||
{
|
||||
Assembly currentAssembly = typeof(Application).Assembly;
|
||||
|
||||
AddEditorContextMenuDefaultStyle(currentAssembly);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user