-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving visual tree extensions out and limiting the search for finding…
… element in error
- Loading branch information
1 parent
238d1e1
commit 270c09e
Showing
5 changed files
with
69 additions
and
61 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/ServiceControl.Config/Extensions/VisualTreeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace ServiceControl.Config.Extensions | ||
{ | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Media; | ||
|
||
public static class VisualTreeExtensions | ||
{ | ||
public static Control FindControlWithError(this DependencyObject parent) | ||
{ | ||
// Confirm parent and childName are valid. | ||
if (parent == null) | ||
{ | ||
return null; | ||
} | ||
|
||
Control foundChild = null; | ||
|
||
var childrenCount = VisualTreeHelper.GetChildrenCount(parent); | ||
for (var i = 0; i < childrenCount; i++) | ||
{ | ||
var child = VisualTreeHelper.GetChild(parent, i); | ||
// If the child is not of the request child type child | ||
var childType = child; | ||
if (childType == null) | ||
{ | ||
// recursively drill down the tree | ||
foundChild = FindControlWithError(child); | ||
|
||
// If the child is found, break so we do not overwrite the found child. | ||
if (foundChild != null) | ||
{ | ||
break; | ||
} | ||
} | ||
else | ||
{ | ||
var frameworkElement = child as FrameworkElement; | ||
|
||
// If the child is in error | ||
if (frameworkElement != null && Validation.GetHasError(frameworkElement)) | ||
{ | ||
foundChild = (Control)child; | ||
break; | ||
} | ||
|
||
// recursively drill down the tree | ||
foundChild = FindControlWithError(child); | ||
|
||
// If the child is found, break so we do not overwrite the found child. | ||
if (foundChild != null) | ||
{ | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return foundChild; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters