MYOBCompanyFileSelectionDialog.xaml.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using MYOB.AccountRight.SDK.Contracts;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. namespace InABox.Poster.MYOB;
  18. /// <summary>
  19. /// Interaction logic for MYOBCompanyFileSelectionDialog.xaml
  20. /// </summary>
  21. public partial class MYOBCompanyFileSelectionDialog : Window, INotifyPropertyChanged
  22. {
  23. private bool _canSave;
  24. public bool CanSave
  25. {
  26. get => _canSave;
  27. set
  28. {
  29. _canSave = true;
  30. OnPropertyChanged();
  31. }
  32. }
  33. public List<MYOBCompanyFile> Items
  34. {
  35. get => Grid.Items;
  36. set => Grid.Items = value;
  37. }
  38. public MYOBCompanyFile? Result
  39. {
  40. get
  41. {
  42. var row = Grid.SelectedRows.FirstOrDefault();
  43. if(row is not null)
  44. {
  45. return Grid.LoadItem(row);
  46. }
  47. return null;
  48. }
  49. }
  50. public MYOBCompanyFileSelectionDialog()
  51. {
  52. InitializeComponent();
  53. }
  54. public event PropertyChangedEventHandler? PropertyChanged;
  55. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  56. {
  57. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  58. }
  59. private void Grid_OnSelectItem(object sender, DynamicGrid.DynamicGridSelectionEventArgs e)
  60. {
  61. CanSave = e.Rows is not null && e.Rows.Length > 0;
  62. }
  63. private void Window_Loaded(object sender, RoutedEventArgs e)
  64. {
  65. Grid.Refresh(true, true);
  66. }
  67. private void CancelButton_Click(object sender, RoutedEventArgs e)
  68. {
  69. DialogResult = false;
  70. Close();
  71. }
  72. private void OKButton_Click(object sender, RoutedEventArgs e)
  73. {
  74. DialogResult = true;
  75. Close();
  76. }
  77. public static CompanyFile? SelectCompanyFile()
  78. {
  79. var data = MYOBPosterEngine.GetConnectionData();
  80. var window = new MYOBCompanyFileSelectionDialog();
  81. var files = data.CompanyFileService.GetRange();
  82. window.Items = files.Select(x =>
  83. {
  84. return new MYOBCompanyFile
  85. {
  86. ID = x.Id,
  87. Name = x.Name
  88. };
  89. }).ToList();
  90. if(window.ShowDialog() == true)
  91. {
  92. var result = window.Result;
  93. if(result is not null)
  94. {
  95. return files.FirstOrDefault(x => x.Id == result.ID);
  96. }
  97. else
  98. {
  99. return null;
  100. }
  101. }
  102. else
  103. {
  104. return null;
  105. }
  106. }
  107. }