编写拖动效果装饰器与状态回显装饰器
This commit is contained in:
@@ -10,19 +10,19 @@ namespace RazorEngineTest
|
|||||||
using System.Windows.Documents;
|
using System.Windows.Documents;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
|
|
||||||
public class PositionAdorner : Adorner
|
public class DropPlacementAdorner : Adorner
|
||||||
{
|
{
|
||||||
private readonly string _position;
|
private readonly string _position;
|
||||||
private static readonly Pen _pen;
|
private static readonly Pen _pen;
|
||||||
|
|
||||||
static PositionAdorner()
|
static DropPlacementAdorner()
|
||||||
{
|
{
|
||||||
// 定义装饰器的画笔
|
// 定义装饰器的画笔
|
||||||
_pen = new Pen(Brushes.Red, 5.0);
|
_pen = new Pen(Brushes.Red, 5.0);
|
||||||
_pen.Freeze();
|
_pen.Freeze();
|
||||||
}
|
}
|
||||||
|
|
||||||
public PositionAdorner(UIElement adornedElement, string position)
|
public DropPlacementAdorner(UIElement adornedElement, string position)
|
||||||
: base(adornedElement)
|
: base(adornedElement)
|
||||||
{
|
{
|
||||||
_position = position;
|
_position = position;
|
||||||
@@ -36,13 +36,11 @@ namespace RazorEngineTest
|
|||||||
// 根据位置信息绘制不同的视觉效果
|
// 根据位置信息绘制不同的视觉效果
|
||||||
switch (_position)
|
switch (_position)
|
||||||
{
|
{
|
||||||
case "前":
|
|
||||||
case "上":
|
case "上":
|
||||||
drawingContext.DrawLine(_pen,
|
drawingContext.DrawLine(_pen,
|
||||||
new Point(0, 0),
|
new Point(0, 0),
|
||||||
new Point(adornedElementRect.Right, 0));
|
new Point(adornedElementRect.Right, 0));
|
||||||
break;
|
break;
|
||||||
case "后":
|
|
||||||
case "下":
|
case "下":
|
||||||
drawingContext.DrawLine(_pen,
|
drawingContext.DrawLine(_pen,
|
||||||
new Point(0, adornedElementRect.Bottom),
|
new Point(0, adornedElementRect.Bottom),
|
||||||
@@ -46,7 +46,7 @@ namespace RazorEngineTest
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class MyControl : Control
|
public class MyControl : Control
|
||||||
{
|
{
|
||||||
private PositionAdorner? _currentAdorner = null;
|
private DropPlacementAdorner? _currentAdorner = null;
|
||||||
private AdornerLayer? _adornerLayer = null;
|
private AdornerLayer? _adornerLayer = null;
|
||||||
static MyControl()
|
static MyControl()
|
||||||
{
|
{
|
||||||
@@ -61,7 +61,7 @@ namespace RazorEngineTest
|
|||||||
|
|
||||||
if (_adornerLayer != null)
|
if (_adornerLayer != null)
|
||||||
{
|
{
|
||||||
_currentAdorner = new PositionAdorner(this, position);
|
_currentAdorner = new DropPlacementAdorner(this, position);
|
||||||
_adornerLayer.Add(_currentAdorner);
|
_adornerLayer.Add(_currentAdorner);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
66
RazorEngineTest/StateAdornerDecorator.cs
Normal file
66
RazorEngineTest/StateAdornerDecorator.cs
Normal file
@@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 按照步骤 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 文件中使用控件。
|
||||||
|
///
|
||||||
|
/// <MyNamespace:StateAdornerDecorator/>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
[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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,10 +14,80 @@
|
|||||||
<Setter Property="Template">
|
<Setter Property="Template">
|
||||||
<Setter.Value>
|
<Setter.Value>
|
||||||
<ControlTemplate TargetType="{x:Type local:MyControl}">
|
<ControlTemplate TargetType="{x:Type local:MyControl}">
|
||||||
<AdornerDecorator>
|
<!--使用「状态装饰器」对象做为模版的根对象,保证当前空间具备装饰层与状态切换动画-->
|
||||||
|
<local:StateAdornerDecorator>
|
||||||
<Border Background="{TemplateBinding Background}">
|
<Border Background="{TemplateBinding Background}">
|
||||||
<TextBlock Text="装饰器测试" VerticalAlignment="Center" HorizontalAlignment="Center"/>
|
<TextBlock Text="装饰器测试" VerticalAlignment="Center" HorizontalAlignment="Center"/>
|
||||||
</Border>
|
</Border>
|
||||||
|
</local:StateAdornerDecorator>
|
||||||
|
</ControlTemplate>
|
||||||
|
</Setter.Value>
|
||||||
|
</Setter>
|
||||||
|
</Style>
|
||||||
|
<Style TargetType="{x:Type local:StateAdornerDecorator}">
|
||||||
|
<Setter Property="Template">
|
||||||
|
<Setter.Value>
|
||||||
|
<ControlTemplate TargetType="{x:Type local:StateAdornerDecorator}">
|
||||||
|
<!--使用「装饰器」容器做为控件模板的根对象,保证当前控件可以为任何控件提供装饰层-->
|
||||||
|
<AdornerDecorator>
|
||||||
|
<Border BorderBrush="{TemplateBinding BorderBrush}"
|
||||||
|
BorderThickness="{TemplateBinding BorderThickness}">
|
||||||
|
<Border.Background>
|
||||||
|
<SolidColorBrush x:Name="BackgroundBrush" Color="White"/>
|
||||||
|
</Border.Background>
|
||||||
|
<VisualStateManager.VisualStateGroups>
|
||||||
|
<VisualStateGroup x:Name="SmartUnit">
|
||||||
|
<VisualState x:Name="Normal">
|
||||||
|
<Storyboard>
|
||||||
|
<!--状态切换动画:背景色、前景色、填充色、描边色、透明度、边框尺寸、描边宽度,边框尺寸-->
|
||||||
|
<ColorAnimation Storyboard.TargetName="BackgroundBrush"
|
||||||
|
Storyboard.TargetProperty="Color"
|
||||||
|
From="White" To="Black"
|
||||||
|
Duration="0:0:3"
|
||||||
|
AutoReverse="True"/>
|
||||||
|
</Storyboard>
|
||||||
|
</VisualState>
|
||||||
|
<VisualState x:Name="Selected">
|
||||||
|
<Storyboard>
|
||||||
|
<!--状态切换动画-->
|
||||||
|
</Storyboard>
|
||||||
|
</VisualState>
|
||||||
|
<VisualState x:Name="NormalHover">
|
||||||
|
<Storyboard>
|
||||||
|
<!--状态切换动画-->
|
||||||
|
</Storyboard>
|
||||||
|
</VisualState>
|
||||||
|
<VisualState x:Name="SelectedHover">
|
||||||
|
<Storyboard>
|
||||||
|
<!--状态切换动画-->
|
||||||
|
</Storyboard>
|
||||||
|
</VisualState>
|
||||||
|
<VisualState x:Name="NormalDragHover">
|
||||||
|
<Storyboard>
|
||||||
|
<!--状态切换动画-->
|
||||||
|
</Storyboard>
|
||||||
|
</VisualState>
|
||||||
|
<VisualState x:Name="SelectedDragHover">
|
||||||
|
<Storyboard>
|
||||||
|
<!--状态切换动画-->
|
||||||
|
</Storyboard>
|
||||||
|
</VisualState>
|
||||||
|
<VisualStateGroup.Transitions>
|
||||||
|
<VisualTransition To="Normal">
|
||||||
|
<Storyboard>
|
||||||
|
<!--状态切换动画:背景色、前景色、填充色、描边色、透明度、边框尺寸、描边宽度,边框尺寸-->
|
||||||
|
<ColorAnimation Storyboard.TargetName="BackgroundBrush"
|
||||||
|
Storyboard.TargetProperty="Color"
|
||||||
|
From="White" To="Black"
|
||||||
|
Duration="0:0:3"
|
||||||
|
AutoReverse="True"/>
|
||||||
|
</Storyboard>
|
||||||
|
</VisualTransition>
|
||||||
|
</VisualStateGroup.Transitions>
|
||||||
|
</VisualStateGroup>
|
||||||
|
</VisualStateManager.VisualStateGroups>
|
||||||
|
<ContentPresenter/>
|
||||||
|
</Border>
|
||||||
</AdornerDecorator>
|
</AdornerDecorator>
|
||||||
</ControlTemplate>
|
</ControlTemplate>
|
||||||
</Setter.Value>
|
</Setter.Value>
|
||||||
|
|||||||
Reference in New Issue
Block a user