You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.0 KiB

  1. using System.Windows.Data;
  2. using System.Windows;
  3. namespace BPA.UIControl
  4. {
  5. /// <summary>
  6. /// 对话框 DataContext 配置
  7. /// </summary>
  8. public class DialogDataContextConfiguration : IDialogDataContextConfiguration
  9. {
  10. private object openParameter;
  11. private IDialogDataContext dialogContext;
  12. private DialogContainer dialog;
  13. private void Dialog_BeforeOpen(object sender, RoutedEventArgs e)
  14. {
  15. dialogContext.OnDialogOpened(openParameter);
  16. }
  17. private void DialogContext_RequestClose(object parameter)
  18. {
  19. DialogContainer.CloseDialogCommand.Execute(parameter, dialog);
  20. }
  21. /// <inheritdoc/>
  22. public bool OnOpenAction(DialogContainer dialog, object content, object parameter)
  23. {
  24. if (content is FrameworkElement element && element.DataContext is IDialogDataContext dialogContext)
  25. {
  26. openParameter = parameter;
  27. this.dialogContext = dialogContext;
  28. this.dialog = dialog;
  29. // 绑定标题
  30. var binding = new Binding(nameof(dialog.Title))
  31. {
  32. Source = dialogContext,
  33. Mode = BindingMode.OneWay
  34. };
  35. dialog.SetBinding(DialogContainer.TitleProperty, binding);
  36. dialog.BeforeOpen += Dialog_BeforeOpen; // 传参到对话框打开 viewmodel
  37. dialogContext.RequestClose += DialogContext_RequestClose; // 设置关闭对话框委托
  38. return true;
  39. }
  40. return false;
  41. }
  42. /// <inheritdoc/>
  43. public void OnCloseAction()
  44. {
  45. if (dialog is { })
  46. {
  47. dialog.BeforeOpen -= Dialog_BeforeOpen;
  48. }
  49. if (dialogContext is { })
  50. {
  51. dialogContext.RequestClose -= DialogContext_RequestClose;
  52. }
  53. openParameter = null;
  54. dialogContext = null;
  55. dialog = null;
  56. }
  57. }
  58. }