I got WPF authentication (added ValidationRulesto the binding) and using the template I can create nice decorations. There are a lot of messages.
But I canβt find a way to display the error message outside the decorated control in a fixed place, for example, TextBlockin the corner of the window, for example.
How could I achieve this? Can I link all my validation error messages to mine DataContext(ViewModel here)?
Update: thanks to the answer, I got part of the job. Verification messages now appear on a different label. Since all text fields with their validation rules are created on the fly along the code, the binding for this is performed as follows:
Binding bindSite = new Binding();
bindSite.Source = this.validationErrorDisplayLabel;
BindingOperations.SetBinding(textBox, Validation.ValidationAdornerSiteProperty, bindSite);
But verification messages are only sent to adornersitefor the last text field for which this code was executed.
I reproduced the problem in this small example.
XAML:
<Grid>
<TextBox
Validation.ValidationAdornerSite="{Binding ElementName=ErrorDisplay}"
HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120">
<TextBox.Text>
<Binding>
<Binding.Path>Box1</Binding.Path>
<Binding.ValidationRules>
<local:RuleA />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBox
Validation.ValidationAdornerSite="{Binding ElementName=ErrorDisplay}"
HorizontalAlignment="Left" Height="23" Margin="10,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120">
<TextBox.Text>
<Binding>
<Binding.Path>Box2</Binding.Path>
<Binding.ValidationRules>
<local:RuleA />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBlock
x:Name="ErrorDisplay"
Background="AntiqueWhite"
Foreground="Red"
Text="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.ValidationAdornerSiteFor).(Validation.Errors)[0].ErrorContent}"
HorizontalAlignment="Left" Margin="230,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="2.218,-4.577" Width="177" Height="51"/>
</Grid>
The class RuleAgenerates a validation error when the value is equal to a string "A". Errors in the second text field are displayed in the TextBlock, errors of the first are not (instead, it uses the default template and gets a red frame).
How can it work for both? The text block does not need to summarize all errors, but display the very first error.