using MAX.Framework.Controls; using MAX.Framework.Controls.Attach; 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; /// /// DialogWindow.xaml 的交互逻辑 /// public partial class DialogWindow : WindowBase, IDialogWindow { private readonly Style dialogWindowStyle; /// /// The of the dialog. /// 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().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().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); } }