From 7212a7dea7d7c2e1d9b12275f959721bc70ddcab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8E=E6=99=BA=E7=BA=AF?= <于智纯@LODESTAR> Date: Sun, 7 Sep 2025 20:41:19 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E5=86=99=E6=8B=96=E5=8A=A8=E6=95=88?= =?UTF-8?q?=E6=9E=9C=E8=A3=85=E9=A5=B0=E5=99=A8=E4=B8=8E=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E5=9B=9E=E6=98=BE=E8=A3=85=E9=A5=B0=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...tionAdorner.cs => DropPlacementAdorner.cs} | 8 +-- RazorEngineTest/MyControl.cs | 4 +- RazorEngineTest/StateAdornerDecorator.cs | 66 +++++++++++++++++ RazorEngineTest/Themes/Generic.xaml | 72 ++++++++++++++++++- 4 files changed, 142 insertions(+), 8 deletions(-) rename RazorEngineTest/{PositionAdorner.cs => DropPlacementAdorner.cs} (90%) create mode 100644 RazorEngineTest/StateAdornerDecorator.cs diff --git a/RazorEngineTest/PositionAdorner.cs b/RazorEngineTest/DropPlacementAdorner.cs similarity index 90% rename from RazorEngineTest/PositionAdorner.cs rename to RazorEngineTest/DropPlacementAdorner.cs index c025cdd..505672a 100644 --- a/RazorEngineTest/PositionAdorner.cs +++ b/RazorEngineTest/DropPlacementAdorner.cs @@ -10,19 +10,19 @@ namespace RazorEngineTest using System.Windows.Documents; using System.Windows.Media; - public class PositionAdorner : Adorner + public class DropPlacementAdorner : Adorner { private readonly string _position; private static readonly Pen _pen; - static PositionAdorner() + static DropPlacementAdorner() { // 定义装饰器的画笔 _pen = new Pen(Brushes.Red, 5.0); _pen.Freeze(); } - public PositionAdorner(UIElement adornedElement, string position) + public DropPlacementAdorner(UIElement adornedElement, string position) : base(adornedElement) { _position = position; @@ -36,13 +36,11 @@ namespace RazorEngineTest // 根据位置信息绘制不同的视觉效果 switch (_position) { - case "前": case "上": drawingContext.DrawLine(_pen, new Point(0, 0), new Point(adornedElementRect.Right, 0)); break; - case "后": case "下": drawingContext.DrawLine(_pen, new Point(0, adornedElementRect.Bottom), diff --git a/RazorEngineTest/MyControl.cs b/RazorEngineTest/MyControl.cs index 43bcd1c..31797f6 100644 --- a/RazorEngineTest/MyControl.cs +++ b/RazorEngineTest/MyControl.cs @@ -46,7 +46,7 @@ namespace RazorEngineTest /// public class MyControl : Control { - private PositionAdorner? _currentAdorner = null; + private DropPlacementAdorner? _currentAdorner = null; private AdornerLayer? _adornerLayer = null; static MyControl() { @@ -61,7 +61,7 @@ namespace RazorEngineTest if (_adornerLayer != null) { - _currentAdorner = new PositionAdorner(this, position); + _currentAdorner = new DropPlacementAdorner(this, position); _adornerLayer.Add(_currentAdorner); } } diff --git a/RazorEngineTest/StateAdornerDecorator.cs b/RazorEngineTest/StateAdornerDecorator.cs new file mode 100644 index 0000000..886780d --- /dev/null +++ b/RazorEngineTest/StateAdornerDecorator.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Markup; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace RazorEngineTest +{ + /// + /// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。 + /// + /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。 + /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 + /// 元素中: + /// + /// xmlns:MyNamespace="clr-namespace:RazorEngineTest" + /// + /// + /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。 + /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 + /// 元素中: + /// + /// xmlns:MyNamespace="clr-namespace:RazorEngineTest;assembly=RazorEngineTest" + /// + /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用, + /// 并重新生成以避免编译错误: + /// + /// 在解决方案资源管理器中右击目标项目,然后依次单击 + /// “添加引用”->“项目”->[浏览查找并选择此项目] + /// + /// + /// 步骤 2) + /// 继续操作并在 XAML 文件中使用控件。 + /// + /// + /// + /// + [TemplateVisualState(GroupName = "SmartUnit", Name = "Normal")] + [TemplateVisualState(GroupName = "SmartUnit", Name = "Selected")] + [TemplateVisualState(GroupName = "SmartUnit", Name = "NormalHover")] + [TemplateVisualState(GroupName = "SmartUnit", Name = "SelectedHover")] + [TemplateVisualState(GroupName = "SmartUnit", Name = "NormalDragHover")] + [TemplateVisualState(GroupName = "SmartUnit", Name = "SelectedDragHover")] + [ContentProperty("Content")] + public class StateAdornerDecorator : ContentControl + { + static StateAdornerDecorator() + { + DefaultStyleKeyProperty.OverrideMetadata(typeof(StateAdornerDecorator), new FrameworkPropertyMetadata(typeof(StateAdornerDecorator))); + } + public StateAdornerDecorator() + { + VisualStateManager.GoToState(this, "Normal", true); + } + } +} diff --git a/RazorEngineTest/Themes/Generic.xaml b/RazorEngineTest/Themes/Generic.xaml index 3ca715f..a73f963 100644 --- a/RazorEngineTest/Themes/Generic.xaml +++ b/RazorEngineTest/Themes/Generic.xaml @@ -14,10 +14,80 @@ - + + + + + + + +