MYOBCompanyFileSelectionDialog.xaml.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. public static CompanyFile? SelectCompanyFile()
  64. {
  65. var data = MYOBPosterEngine.GetConnectionData();
  66. var window = new MYOBCompanyFileSelectionDialog();
  67. var files = data.CompanyFileService.GetRange();
  68. window.Items = files.Select(x =>
  69. {
  70. return new MYOBCompanyFile
  71. {
  72. ID = x.Id,
  73. Name = x.Name
  74. };
  75. }).ToList();
  76. if(window.ShowDialog() == true)
  77. {
  78. var result = window.Result;
  79. if(result is not null)
  80. {
  81. return files.FirstOrDefault(x => x.Id == result.ID);
  82. }
  83. else
  84. {
  85. return null;
  86. }
  87. }
  88. else
  89. {
  90. return null;
  91. }
  92. }
  93. private void Window_Loaded(object sender, RoutedEventArgs e)
  94. {
  95. Grid.Refresh(true, true);
  96. }
  97. }