improved dialog
This commit is contained in:
1 parent
76a886c331
commit
745909f485
16 files changed
+219
-11
No files matched your search
@@ -1,4 +1,6 @@
|
||||
using System.Windows;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
@@ -12,6 +14,8 @@ namespace HandyControl.Controls
|
||||
{
|
||||
private Adorner _container;
|
||||
|
||||
private static readonly Dictionary<string, System.Windows.Window> WindowDic = new Dictionary<string, System.Windows.Window>();
|
||||
|
||||
public static readonly DependencyProperty IsClosedProperty = DependencyProperty.Register(
|
||||
"IsClosed", typeof(bool), typeof(Dialog), new PropertyMetadata(ValueBoxes.FalseBox));
|
||||
|
||||
@@ -21,21 +25,94 @@ namespace HandyControl.Controls
|
||||
internal set => SetValue(IsClosedProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty TokenProperty = DependencyProperty.RegisterAttached(
|
||||
"Token", typeof(string), typeof(Dialog), new PropertyMetadata(default(string), OnTokenChanged));
|
||||
|
||||
private static void OnTokenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is System.Windows.Window window)
|
||||
{
|
||||
if (e.NewValue == null)
|
||||
{
|
||||
Unregister(window);
|
||||
}
|
||||
else
|
||||
{
|
||||
Register(e.NewValue.ToString(), window);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetToken(DependencyObject element, string value)
|
||||
=> element.SetValue(TokenProperty, value);
|
||||
|
||||
public static string GetToken(DependencyObject element)
|
||||
=> (string) element.GetValue(TokenProperty);
|
||||
|
||||
public Dialog()
|
||||
{
|
||||
CommandBindings.Add(new CommandBinding(ControlCommands.Close, (s, e) => Close()));
|
||||
}
|
||||
|
||||
public static Dialog Show<T>() where T : new() => Show(new T());
|
||||
public static void Register(string token, System.Windows.Window window)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token) || window == null) return;
|
||||
WindowDic[token] = window;
|
||||
}
|
||||
|
||||
public static Dialog Show(object content)
|
||||
public static void Unregister(string token, System.Windows.Window window)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token) || window == null) return;
|
||||
|
||||
if (WindowDic.ContainsKey(token))
|
||||
{
|
||||
if (ReferenceEquals(WindowDic[token], window))
|
||||
{
|
||||
WindowDic.Remove(token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Unregister(System.Windows.Window window)
|
||||
{
|
||||
if (window == null) return;
|
||||
var first = WindowDic.FirstOrDefault(item => ReferenceEquals(window, item.Value));
|
||||
if (!string.IsNullOrEmpty(first.Key))
|
||||
{
|
||||
WindowDic.Remove(first.Key);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Unregister(string token)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token)) return;
|
||||
|
||||
if (WindowDic.ContainsKey(token))
|
||||
{
|
||||
WindowDic.Remove(token);
|
||||
}
|
||||
}
|
||||
|
||||
public static Dialog Show<T>(string token = "") where T : new() => Show(new T(), token);
|
||||
|
||||
public static Dialog Show(object content, string token = "")
|
||||
{
|
||||
var dialog = new Dialog
|
||||
{
|
||||
Content = content
|
||||
};
|
||||
|
||||
var window = VisualHelper.GetActiveWindow();
|
||||
System.Windows.Window window;
|
||||
|
||||
if (string.IsNullOrEmpty(token))
|
||||
{
|
||||
window = VisualHelper.GetActiveWindow();
|
||||
}
|
||||
else
|
||||
{
|
||||
WindowDic.TryGetValue(token, out window);
|
||||
}
|
||||
|
||||
if (window != null)
|
||||
{
|
||||
var decorator = VisualHelper.GetChild<AdornerDecorator>(window);
|
||||
@@ -62,9 +139,23 @@ namespace HandyControl.Controls
|
||||
return dialog;
|
||||
}
|
||||
|
||||
public void Close()
|
||||
public void Close(string token = "")
|
||||
{
|
||||
if (!string.IsNullOrEmpty(token))
|
||||
{
|
||||
if (WindowDic.TryGetValue(token, out var window))
|
||||
{
|
||||
Close(window);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Close(VisualHelper.GetActiveWindow());
|
||||
}
|
||||
}
|
||||
|
||||
private void Close(System.Windows.Window window)
|
||||
{
|
||||
var window = VisualHelper.GetActiveWindow();
|
||||
if (window != null)
|
||||
{
|
||||
var decorator = VisualHelper.GetChild<AdornerDecorator>(window);
|
||||
|
||||
@@ -98,6 +98,19 @@ namespace HandyControl.Controls
|
||||
}
|
||||
}
|
||||
|
||||
public static void Unregister(string token)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token)) return;
|
||||
|
||||
if (PanelDic.ContainsKey(token))
|
||||
{
|
||||
var panel = PanelDic[token];
|
||||
PanelDic.Remove(token);
|
||||
panel.ContextMenu = null;
|
||||
panel.SetCurrentValue(PanelElement.FluidMoveBehaviorProperty, DependencyProperty.UnsetValue);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnMouseEnter(MouseEventArgs e)
|
||||
{
|
||||
base.OnMouseEnter(e);
|
||||
|
||||
Reference in new issue
Block a user