Files
2026-10-28 17:56:50 +08:00

147 lines
3.7 KiB
C#

using Prism.Services.Dialogs;
using Store.Management.Framework.Controls;
using Store.Management.Framework.Controls.Attach;
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;
using MAX.Framework.Extensions;
namespace Store.Management.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();
if (Application.Current.MainWindow is Window owner && !ReferenceEquals(owner, this))
{
Owner = owner;
}
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)
{
// InitializeComponent 设置初始内容时,窗口样式尚未加载。
if (!isConstructorSuccessful)
{
base.OnContentChanged(oldContent, newContent);
return;
}
this.Style = dialogWindowStyle;
if (newContent is UserControl control)
{
WindowStartupLocation = DialogWindowProperty.GetWindowStartupLocation(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 && !ReferenceEquals(dialogWindowStyle, newStyle))
{
dialogWindowStyle.Merge(newStyle);
}
return;
}
base.OnStyleChanged(oldStyle, newStyle);
}
}