/CompDialog.xaml.cs
C# | 71 lines | 51 code | 4 blank | 16 comment | 8 complexity | f15fe39f8ae9fe271fa0f060b47a7aeb MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
-
- namespace QuickArch
- {
- /// <summary>
- /// Interaction logic for Window1.xaml
- /// </summary>
- public partial class ComponentDialog : Window
- {
- public ComponentDialog()
- {
- InitializeComponent();
- }
- void okButton_Click(object sender, RoutedEventArgs e)
- {
- // Don't accept the dialog box if there is invalid data
- if (!IsValid(this)) return;
-
- // Dialog box accepted
- this.DialogResult = true;
- }
- void cancelButton_Click(object sender, RoutedEventArgs e)
- {
- // Dialog box canceled
- this.DialogResult = false;
- }
- // Validate all dependency objects in a window
- bool IsValid(DependencyObject node)
- {
- // Check if dependency object was passed
- if (node != null)
- {
- // Check if dependency object is valid.
- // NOTE: Validation.GetHasError works for controls that have validation rules attached
- bool isValid = !Validation.GetHasError(node);
- if (!isValid)
- {
- // If the dependency object is invalid, and it can receive the focus,
- // set the focus
- if (node is IInputElement) Keyboard.Focus((IInputElement)node);
- return false;
- }
- }
-
- // If this dependency object is valid, check all child dependency objects
- foreach (object subnode in LogicalTreeHelper.GetChildren(node))
- {
- if (subnode is DependencyObject)
- {
- // If a child dependency object is invalid, return false immediately,
- // otherwise keep checking
- if (IsValid((DependencyObject)subnode) == false) return false;
- }
- }
-
- // All dependency objects are valid
- return true;
- }
- }
- }