punker76 / MahApps.Metro.SimpleChildWindow

A simple child window for MahApps.Metro
MIT License
374 stars 55 forks source link

Validation Errors not displayed until user makes edits in child window #59

Closed DoozerRHS closed 5 years ago

DoozerRHS commented 6 years ago

I love the child-window feature, but I can't get initial-validation-state-feedback to work.... I think it is a bug, or maybe I need advice on the usage of a property or feature?

Background I have a view model, which implements INotifyDataErrorInfo, it has several properties and some validation conditions which cause the HasErrors flag to be set true, and the GetErrors function to return some results for some propeties

The problem If I show a child window and set it's data context to an instance of the view-model with validation errors present they are not reported in the UI (no red borders and no fly-outs with text for the error details). e.g. for a textbox bound to a property in error. When a user edits a text value so that it becomes valid, and then changes it again so it becomes invalid the red-border and text-flyout is seen. This indicates that it is not a template problem with missing AdornerDecorator.

After a bit of googling I have tried several routes to launch the child window, to see if this would make the error validation show when displayed (all have the same effect of "window is displayed, expected validation errors are not") including:

  1. Window is created as child of the main Mah Apps MetroWindow (the main window) via definition in the xaml where IsOpen bound to the property holding the instance of the view model - which is normally null, but set on user commencing an edit.
  2. Create new window instance from code behind, use data-bindings to set its data context.
  3. Create new window instance from code behind, set data context using code behind.
  4. Create new window instance from code behind, set data context in a child window's On Loaded event handler.

If I change my child window type from "ChildWindow" to be a regular "MetroWindow" or standard WPF "Window" the initial validation errors are observed as expected.

I am using the latest versions of the ChildWndow and MahApp theme libraries.

Any advice would be gratefully received.

punker76 commented 6 years ago

@DoozerRHS It would be nice if you can create a simple app which demonstrates this issue. thx.

DoozerRHS commented 6 years ago

I have started to put a test program together, and can provide some more information, I discovered: The child-window will display the validation indicators if the content is wrapped within an AdornerDecorator. If the child window contains a user-control, the user-control itself must have its content wrapped within an AdorderDecorator. So I have a work-around to add AdornerDecorators into my XAML.

However if using MetroWindow or regular WPF Window's I don't need to add in additional AdorderDecorators.

Sample attached, with buttons for different tests. Tests 1 & 2 show "correct" behavior without using Mah apps theming controls (normal WPF windows) Tests 3 & 4 show "correct" behavior using Mah Apps MetroWindow Test 5, 6, 7 show "problem" behavior Test 8 shows work-around.

My hunch is that there is a subtle (maybe templating) bug, probably due to some complicated expectation from the WPF on how Adorners for validation are supposed to work / not inherited - but this is not an area I know much about.

Sample Attached: DemoValidationIssue.zip

punker76 commented 6 years ago

@DoozerRHS Ok, cool. And using an AdornerDecorator at this point is also the solution for this issue :-D