PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/CheckListWindow.xaml.cs

https://bitbucket.org/rstarkov/tankiconmaker
C# | 95 lines | 83 code | 12 blank | 0 comment | 14 complexity | 3fb601d266f15062e05f0597ba245588 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-3.0, CC-BY-SA-3.0
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using RT.Util.Dialogs;
  9. using RT.Util.Forms;
  10. using WpfCrutches;
  11. namespace TankIconMaker
  12. {
  13. partial class CheckListWindow : ManagedWindow
  14. {
  15. private ObservableCollection<CheckListItem> _checkItems;
  16. private ObservableValue<bool?> _checkAll = new ObservableValue<bool?>(null);
  17. private string _promptSure;
  18. private string _promptSureYes;
  19. private CheckListWindow()
  20. : base(App.Settings.CheckListWindow)
  21. {
  22. InitializeComponent();
  23. MainWindow.ApplyUiZoom(this);
  24. }
  25. private void ok(object sender, RoutedEventArgs e)
  26. {
  27. if (_promptSure != null && _checkItems.Count(item => item.IsChecked) > 0)
  28. if (DlgMessage.ShowWarning(_promptSure, _promptSureYes, App.Translation.Prompt.Cancel) != 0)
  29. return;
  30. DialogResult = true;
  31. }
  32. private void setAllCheckboxes(bool? check)
  33. {
  34. if (check == null)
  35. return;
  36. foreach (var item in _checkItems)
  37. item.IsChecked = check.Value;
  38. }
  39. private void checkedChanged(object sender, RoutedEventArgs e)
  40. {
  41. int checkedCount = _checkItems.Count(item => item.IsChecked);
  42. _checkAll.Value = checkedCount == 0 ? false : checkedCount == _checkItems.Count ? true : (bool?) null;
  43. }
  44. public static IEnumerable<TItem> ShowCheckList<TItem>(Window owner, IEnumerable<CheckListItem<TItem>> items, string prompt, string okButton, string[] columnTitles, string promptSure = null)
  45. {
  46. var wnd = new CheckListWindow() { Owner = owner };
  47. wnd.ctPrompt.Text = prompt;
  48. wnd.ctOkBtn.Text = okButton;
  49. wnd.ctCancelBtn.Text = App.Translation.Prompt.Cancel;
  50. for (int i = 0; i < columnTitles.Length; i++)
  51. {
  52. wnd.ctGrid.Columns[i + 1].Header = columnTitles[i];
  53. wnd.ctGrid.Columns[i + 1].Visibility = Visibility.Visible;
  54. if (i != columnTitles.Length - 1) // mark all columns except for the last one as auto width
  55. wnd.ctGrid.Columns[i + 1].Width = DataGridLength.Auto;
  56. }
  57. wnd._promptSure = promptSure;
  58. wnd._promptSureYes = okButton.Replace('_', '&');
  59. wnd._checkItems = new ObservableCollection<CheckListItem>(items);
  60. wnd.ctGrid.ItemsSource = wnd._checkItems;
  61. BindingOperations.SetBinding(wnd.chkSelectAll, CheckBox.IsCheckedProperty, LambdaBinding.New(
  62. new Binding { Source = wnd._checkAll, Path = new PropertyPath("Value") },
  63. (bool? checkAll) => checkAll,
  64. (bool? checkAll) => { wnd.setAllCheckboxes(checkAll); return checkAll; }
  65. ));
  66. if (wnd.ShowDialog() != true)
  67. return Enumerable.Empty<TItem>();
  68. return wnd._checkItems.OfType<CheckListItem<TItem>>().Where(cli => cli.IsChecked).Select(cli => cli.Item);
  69. }
  70. }
  71. public class CheckListItem : INotifyPropertyChanged
  72. {
  73. public string Column1 { get; set; }
  74. public string Column2 { get; set; }
  75. public bool IsChecked { get { return _isChecked; } set { _isChecked = value; PropertyChanged(this, new PropertyChangedEventArgs("IsChecked")); } }
  76. private bool _isChecked;
  77. public event PropertyChangedEventHandler PropertyChanged = delegate { };
  78. }
  79. public class CheckListItem<TItem> : CheckListItem
  80. {
  81. public TItem Item { set; get; }
  82. }
  83. }