PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/CompDialog.xaml.cs

https://github.com/shader/QuickArch
C# | 71 lines | 51 code | 4 blank | 16 comment | 8 complexity | f15fe39f8ae9fe271fa0f060b47a7aeb MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13. namespace QuickArch
  14. {
  15. /// <summary>
  16. /// Interaction logic for Window1.xaml
  17. /// </summary>
  18. public partial class ComponentDialog : Window
  19. {
  20. public ComponentDialog()
  21. {
  22. InitializeComponent();
  23. }
  24. void okButton_Click(object sender, RoutedEventArgs e)
  25. {
  26. // Don't accept the dialog box if there is invalid data
  27. if (!IsValid(this)) return;
  28. // Dialog box accepted
  29. this.DialogResult = true;
  30. }
  31. void cancelButton_Click(object sender, RoutedEventArgs e)
  32. {
  33. // Dialog box canceled
  34. this.DialogResult = false;
  35. }
  36. // Validate all dependency objects in a window
  37. bool IsValid(DependencyObject node)
  38. {
  39. // Check if dependency object was passed
  40. if (node != null)
  41. {
  42. // Check if dependency object is valid.
  43. // NOTE: Validation.GetHasError works for controls that have validation rules attached
  44. bool isValid = !Validation.GetHasError(node);
  45. if (!isValid)
  46. {
  47. // If the dependency object is invalid, and it can receive the focus,
  48. // set the focus
  49. if (node is IInputElement) Keyboard.Focus((IInputElement)node);
  50. return false;
  51. }
  52. }
  53. // If this dependency object is valid, check all child dependency objects
  54. foreach (object subnode in LogicalTreeHelper.GetChildren(node))
  55. {
  56. if (subnode is DependencyObject)
  57. {
  58. // If a child dependency object is invalid, return false immediately,
  59. // otherwise keep checking
  60. if (IsValid((DependencyObject)subnode) == false) return false;
  61. }
  62. }
  63. // All dependency objects are valid
  64. return true;
  65. }
  66. }
  67. }