diff --git a/src/ServiceControl.Config/Extensions/VisualTreeExtensions.cs b/src/ServiceControl.Config/Extensions/VisualTreeExtensions.cs
new file mode 100644
index 0000000000..76d65ea1e5
--- /dev/null
+++ b/src/ServiceControl.Config/Extensions/VisualTreeExtensions.cs
@@ -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;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/ServiceControl.Config/Framework/WindowManagerEx.cs b/src/ServiceControl.Config/Framework/WindowManagerEx.cs
index 7d26ffd7b4..74c29884b8 100644
--- a/src/ServiceControl.Config/Framework/WindowManagerEx.cs
+++ b/src/ServiceControl.Config/Framework/WindowManagerEx.cs
@@ -9,9 +9,7 @@
namespace ServiceControl.Config.Framework
{
- using System.Linq;
- using System.Windows.Controls;
- using System.Windows.Media;
+ using ServiceControl.Config.Extensions;
public interface IWindowManagerEx : IWindowManager
{
@@ -29,7 +27,7 @@ public interface IWindowManagerEx : IWindowManager
bool ShowActionReport(ReportCard reportcard, string title, string errorsMessage, string warningsMessage);
- void ScrollFirstErrorIntoView();
+ void ScrollFirstErrorIntoView(object viewModel, object context = null);
}
class WindowManagerEx : WindowManager, IWindowManagerEx
@@ -111,65 +109,13 @@ public bool ShowActionReport(ReportCard reportcard, string title, string errorsM
return result ?? false;
}
- public void ScrollFirstErrorIntoView()
+ public void ScrollFirstErrorIntoView(object viewModel, object context = null)
{
- var controlInError = FindChild(Application.Current.MainWindow);
+ var view = ViewLocator.LocateForModel(viewModel, null, context);
+ var controlInError = view?.FindControlWithError();
controlInError?.BringIntoView();
}
- static Control FindChild(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 = FindChild(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 = FindChild(child);
-
- // If the child is found, break so we do not overwrite the found child.
- if (foundChild != null)
- {
- break;
- }
-
- }
- }
-
- return foundChild;
- }
-
private ShellViewModel GetShell()
{
var shell = Application.Current.MainWindow.DataContext as ShellViewModel;
diff --git a/src/ServiceControl.Config/ServiceControl.Config.csproj b/src/ServiceControl.Config/ServiceControl.Config.csproj
index 664cd306a3..793d224c29 100644
--- a/src/ServiceControl.Config/ServiceControl.Config.csproj
+++ b/src/ServiceControl.Config/ServiceControl.Config.csproj
@@ -150,6 +150,7 @@
+
diff --git a/src/ServiceControl.Config/UI/InstanceAdd/InstanceAddAttachment.cs b/src/ServiceControl.Config/UI/InstanceAdd/InstanceAddAttachment.cs
index 5e6c5be72b..4ca0aa2933 100644
--- a/src/ServiceControl.Config/UI/InstanceAdd/InstanceAddAttachment.cs
+++ b/src/ServiceControl.Config/UI/InstanceAdd/InstanceAddAttachment.cs
@@ -48,7 +48,7 @@ private async Task Add(object arg)
{
viewModel.NotifyOfPropertyChange(string.Empty);
viewModel.SubmitAttempted = false;
- windowManager.ScrollFirstErrorIntoView();
+ windowManager.ScrollFirstErrorIntoView(viewModel);
return;
}
diff --git a/src/ServiceControl.Config/UI/InstanceEdit/InstanceEditAttachment.cs b/src/ServiceControl.Config/UI/InstanceEdit/InstanceEditAttachment.cs
index a72b8f331e..a4bd624991 100644
--- a/src/ServiceControl.Config/UI/InstanceEdit/InstanceEditAttachment.cs
+++ b/src/ServiceControl.Config/UI/InstanceEdit/InstanceEditAttachment.cs
@@ -49,7 +49,7 @@ async Task Save(object arg)
{
viewModel.NotifyOfPropertyChange(string.Empty);
viewModel.SubmitAttempted = false;
- windowManager.ScrollFirstErrorIntoView();
+ windowManager.ScrollFirstErrorIntoView(viewModel);
return;
}