Display IDataErrorInfo Error in Textblock instead of Tooltip
-
04-06-2021 - |
Question
I've seen lots of examples of how to display an error in a tooltip of a textbox. Can someone give me an example of how to display an error in a separate TextBlock instead of in a tooltip of the TextBox?
I'm particularly interested in knowing how to do this if the textbox and textblock are in different views. The view with the textblock has access to the data that has IDataErrorInfo implemented on it though.
Solution 2
So what I ended up doing is adding a property that I set with the Error text. Anytime my Name property is updated, if it has an error, I update the NameError property. I was then able to bind to that property. It seems a little dirty but it did the trick for me.
OTHER TIPS
you wrote
The view with the textblock has access to the data that has IDataErrorInfo implemented on it though.
so one way could be to just bind the textblock to the same property like your textbox do. then create a validationtemplate for your textblock wich cover the whole textblock.
EDIT: maybe something like this?
<ControlTemplate x:Key="ValidationTemplate">
<Grid>
<AdornedElementPlaceholder Name="MyAdornedElement" />
<TextBlock Text="{Binding ElementName=MyAdornedElement,Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}" />
</Grid>
</ControlTemplate>
you have to add this to the textblock style of course