optimized drawer

This commit is contained in:
NaBian committed 2020-06-01 23:08:26 +08:00
1 parent 27c9458130
commit 3dad11526c
3 files changed
+28 -7

No files matched your search

@@ -1,6 +1,7 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
@@ -37,12 +38,15 @@ namespace HandyControl.Controls
private Point _contentRenderTransformOrigin;
static Drawer()
{
DataContextProperty.OverrideMetadata(typeof(Drawer), new FrameworkPropertyMetadata(DataContextPropertyChanged));
}
public Drawer()
{
Loaded += Drawer_Loaded;
Unloaded += Drawer_Unloaded;
CommandBindings.Add(new CommandBinding(ControlCommands.Close, (s, e) => SetCurrentValue(IsOpenProperty, ValueBoxes.FalseBox)));
}
private void Drawer_Loaded(object sender, RoutedEventArgs e)
@@ -68,6 +72,11 @@ namespace HandyControl.Controls
}
}
private static void DataContextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
((Drawer)d).OnDataContextPropertyChanged(e);
private void OnDataContextPropertyChanged(DependencyPropertyChangedEventArgs e) => UpdateDataContext(_animationControl, e.OldValue, e.NewValue);
public static readonly RoutedEvent OpenedEvent =
EventManager.RegisterRoutedEvent("Opened", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(Drawer));
@@ -223,6 +232,9 @@ namespace HandyControl.Controls
throw new ArgumentOutOfRangeException();
}
_animationControl.DataContext = DataContext;
_animationControl.CommandBindings.Clear();
_animationControl.CommandBindings.Add(new CommandBinding(ControlCommands.Close, (s, e) => SetCurrentValue(IsOpenProperty, ValueBoxes.FalseBox)));
panel.Children.Add(_animationControl);
_container = new AdornerContainer(_layer)
{
@@ -381,5 +393,14 @@ namespace HandyControl.Controls
SetCurrentValue(IsOpenProperty, ValueBoxes.FalseBox);
}
}
private void UpdateDataContext(FrameworkElement target, object oldValue, object newValue)
{
if (target == null || BindingOperations.GetBindingExpression(target, DataContextProperty) != null) return;
if (ReferenceEquals(this, target.DataContext) || Equals(oldValue, target.DataContext))
{
target.DataContext = newValue ?? this;
}
}
}
}