Files
BrightSharp.Toolkit/BrightSharp/Diagrams/DesignerItemDecorator.cs

104 lines
3.2 KiB
C#
Raw Normal View History

2017-01-09 13:25:52 +03:00
using System;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
2017-02-18 00:15:59 +03:00
using System.ComponentModel;
2017-01-09 13:25:52 +03:00
namespace BrightSharp.Diagrams
2017-01-09 13:25:52 +03:00
{
2017-02-18 00:15:59 +03:00
[ToolboxItem(false)]
2017-01-09 13:25:52 +03:00
public class DesignerItemDecorator : Control
{
private Adorner adorner;
public bool ShowDecorator
{
get { return (bool)GetValue(ShowDecoratorProperty); }
set { SetValue(ShowDecoratorProperty, value); }
}
public static readonly DependencyProperty ShowDecoratorProperty =
DependencyProperty.Register("ShowDecorator", typeof(bool), typeof(DesignerItemDecorator),
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(ShowDecoratorProperty_Changed)));
public DesignerItemDecorator()
{
Unloaded += new RoutedEventHandler(DesignerItemDecorator_Unloaded);
2017-01-09 13:25:52 +03:00
}
private void HideAdorner()
{
if (adorner != null)
2017-01-09 13:25:52 +03:00
{
adorner.Visibility = Visibility.Hidden;
2017-01-09 13:25:52 +03:00
}
}
private void ShowAdorner()
{
if (adorner == null)
2017-01-09 13:25:52 +03:00
{
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this);
if (adornerLayer != null)
{
ContentControl designerItem = DataContext as ContentControl;
2017-01-09 13:25:52 +03:00
Canvas canvas = VisualTreeHelper.GetParent(designerItem) as Canvas;
adorner = new ResizeRotateAdorner(designerItem);
adornerLayer.Add(adorner);
2017-01-09 13:25:52 +03:00
if (ShowDecorator)
2017-01-09 13:25:52 +03:00
{
adorner.Visibility = Visibility.Visible;
2017-01-09 13:25:52 +03:00
var anim = new DoubleAnimation(0, 1, TimeSpan.FromSeconds(.2));
adorner.BeginAnimation(OpacityProperty, anim);
2017-01-09 13:25:52 +03:00
}
else
{
adorner.Visibility = Visibility.Hidden;
2017-01-09 13:25:52 +03:00
}
}
}
else
{
adorner.Visibility = Visibility.Visible;
2017-01-09 13:25:52 +03:00
var anim = new DoubleAnimation(0, 1, TimeSpan.FromSeconds(.2));
adorner.BeginAnimation(OpacityProperty, anim);
2017-01-09 13:25:52 +03:00
}
}
private void DesignerItemDecorator_Unloaded(object sender, RoutedEventArgs e)
{
if (adorner != null)
2017-01-09 13:25:52 +03:00
{
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this);
if (adornerLayer != null)
{
adornerLayer.Remove(adorner);
2017-01-09 13:25:52 +03:00
}
adorner = null;
2017-01-09 13:25:52 +03:00
}
}
private static void ShowDecoratorProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DesignerItemDecorator decorator = (DesignerItemDecorator)d;
bool showDecorator = (bool)e.NewValue;
if (showDecorator)
{
decorator.ShowAdorner();
}
else
{
decorator.HideAdorner();
}
}
}
}