135 lines
3.3 KiB
C#
135 lines
3.3 KiB
C#
using MAX.Framework.Controls;
|
|
using MAX.Framework.Controls.Attach;
|
|
using MAX.Framework.Extensions;
|
|
using Prism.Services.Dialogs;
|
|
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.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Shapes;
|
|
|
|
namespace Trade.Framework.Controls;
|
|
|
|
/// <summary>
|
|
/// DialogWindow.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class DialogWindow : WindowBase, IDialogWindow
|
|
{
|
|
private readonly Style dialogWindowStyle;
|
|
|
|
/// <summary>
|
|
/// The <see cref="IDialogResult"/> of the dialog.
|
|
/// </summary>
|
|
public IDialogResult Result { get; set; }
|
|
private bool isConstructorSuccessful = false;
|
|
public DialogWindow()
|
|
{
|
|
InitializeComponent();
|
|
Owner = Application.Current.MainWindow;
|
|
isConstructorSuccessful = true;
|
|
dialogWindowStyle = (Style)TryFindResource("DialogWindowStyle");
|
|
|
|
{
|
|
RoutedCommand escapeCommand = new RoutedCommand();
|
|
CommandBindings.Add(new CommandBinding(escapeCommand, new ExecutedRoutedEventHandler((s, e) =>
|
|
{
|
|
if (DataContext is ConfirmDialogViewModel vm && vm.AllowEscape)
|
|
{
|
|
vm.CancelCommand?.Execute(null);
|
|
}
|
|
})));
|
|
InputBindings.Add(new InputBinding(escapeCommand, new KeyGesture(Key.Escape)));
|
|
}
|
|
|
|
{
|
|
RoutedCommand enterCommand = new RoutedCommand();
|
|
CommandBindings.Add(new CommandBinding(enterCommand, new ExecutedRoutedEventHandler((s, e) =>
|
|
{
|
|
if (DataContext is ConfirmDialogViewModel vm && vm.AllowEnter)
|
|
{
|
|
vm.ConfirmCommand?.Execute(null);
|
|
}
|
|
})));
|
|
InputBindings.Add(new InputBinding(enterCommand, new KeyGesture(Key.Enter)));
|
|
}
|
|
|
|
Loaded += DialogWindow_Loaded;
|
|
}
|
|
|
|
private void DialogWindow_Loaded(object sender, RoutedEventArgs e)
|
|
{
|
|
Loaded -= DialogWindow_Loaded;
|
|
if (DataContext is ConfirmDialogViewModel viewModel)
|
|
{
|
|
viewModel.Loaded(Content);
|
|
}
|
|
}
|
|
|
|
protected override void OnContentChanged(object oldContent, object newContent)
|
|
{
|
|
this.Style = dialogWindowStyle;
|
|
if (newContent is UserControl control)
|
|
{
|
|
|
|
Setter widthSetter = this.Style.Setters.OfType<Setter>().FirstOrDefault(a => a.Property == WidthProperty);
|
|
if (widthSetter == null)
|
|
{
|
|
if (double.IsNaN(control.Width))
|
|
{
|
|
this.Width = DialogWindowProperty.GetWidth(control);
|
|
}
|
|
else
|
|
{
|
|
this.Width = control.Width;
|
|
control.Width = double.NaN;
|
|
}
|
|
}
|
|
|
|
|
|
Setter heightSetter = this.Style.Setters.OfType<Setter>().FirstOrDefault(a => a.Property == HeightProperty);
|
|
if (heightSetter == null)
|
|
{
|
|
if (double.IsNaN(control.Height))
|
|
{
|
|
this.Height = DialogWindowProperty.GetHeight(control);
|
|
}
|
|
else
|
|
{
|
|
this.Height = control.Height;
|
|
control.Height = double.NaN;
|
|
}
|
|
}
|
|
|
|
|
|
ResizeMode resizeExt = DialogWindowProperty.GetResizeMode(control);
|
|
if (resizeExt != ResizeMode.CanResize)
|
|
{
|
|
this.ResizeMode = resizeExt;
|
|
}
|
|
|
|
}
|
|
base.OnContentChanged(oldContent, newContent);
|
|
}
|
|
|
|
protected override void OnStyleChanged(Style oldStyle, Style newStyle)
|
|
{
|
|
if (newStyle.TargetType == typeof(Window))
|
|
{
|
|
if (isConstructorSuccessful)
|
|
{
|
|
dialogWindowStyle.Merge(newStyle);
|
|
}
|
|
return;
|
|
}
|
|
base.OnStyleChanged(oldStyle, newStyle);
|
|
}
|
|
}
|