I want to display the errors (IDataErrorInfo.Errors) in One place of screen instead of showing Error Content near by.. So For that I have placed textBlock in End of the Form.. How Can I Get Current Focused Element for Binding (Validation.Errors)[0].ErrorContent.
this should be done in XAML not in Code behind.
When Focus Changed then That Element’s Error content is displayed in that TextBlock placed bottom of the screen..
Thanks & Regards
Dineshbabu Sengottian
You can access the focused element using
FocusManager.FocusedElement. Here is an example that works purely with XAML, without any code-behind (except of course for the code-behind necessary to provideIDataErrorInfoerrors for testing):The test class
MainWindowhas the following code:For testing, you get a validation error if you put “x” in the first text box or if you put “y” in the second text box.
The error message of the currently focused text box appears below the two text boxes in the TextBlock.
Please note that this solution has one drawback. If you run the sample under the debugger, you will see those binding errors:
These debug error messages occur when the currently focused element has not validation error, because the
Validation.Errorsarray is empty, and therefore[0]is illegal.You can choose either to ignore those error messages (the sample still runs fine), or you need some code-behind nevertheless, e.g. a converter that converts the
IInputElementreturned fromFocusManager.FocusedElementto a string.