Skip to content

Commit

Permalink
Moving visual tree extensions out and limiting the search for finding…
Browse files Browse the repository at this point in the history
… element in error
  • Loading branch information
HEskandari committed Jun 8, 2016
1 parent 238d1e1 commit 270c09e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 61 deletions.
61 changes: 61 additions & 0 deletions src/ServiceControl.Config/Extensions/VisualTreeExtensions.cs
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;
}
}
}
64 changes: 5 additions & 59 deletions src/ServiceControl.Config/Framework/WindowManagerEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/ServiceControl.Config/ServiceControl.Config.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
<Compile Include="Extensions\DeactivateExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Extensions\ValidatorExtensions.cs" />
<Compile Include="Extensions\VisualTreeExtensions.cs" />
<Compile Include="Framework\Commands\AwaitableAbstractCommand.cs" />
<Compile Include="Framework\IProgressViewModel.cs" />
<Compile Include="Framework\Modules\InstallerModule.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private async Task Add(object arg)
{
viewModel.NotifyOfPropertyChange(string.Empty);
viewModel.SubmitAttempted = false;
windowManager.ScrollFirstErrorIntoView();
windowManager.ScrollFirstErrorIntoView(viewModel);

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async Task Save(object arg)
{
viewModel.NotifyOfPropertyChange(string.Empty);
viewModel.SubmitAttempted = false;
windowManager.ScrollFirstErrorIntoView();
windowManager.ScrollFirstErrorIntoView(viewModel);

return;
}
Expand Down

0 comments on commit 270c09e

Please sign in to comment.