0

I have a user control that consists of a textblock and a textbox and if I validate via INotifyDataErrorInfo the underlying data the whole control gets a red border: How it looks like

I would like to use the textbox to show something is wrong in my user controls data, as if I had used a textbox instead of my control. In my case I'm using wpf material design so it looks like that.

My controls xaml:

<UserControl  ....>
    <Grid Height="40" Width="380" Margin="0,1,0,1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="110"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding ElementName=LabelWithInputUC,Path=Label}" Grid.Column="0" Margin="0,4,0,0" VerticalAlignment="Bottom"/>

            <TextBox VerticalAlignment="Bottom"
                     Margin="10,0,30,0" Grid.Column="1"
                     x:Name="ContentTextBox"
                     MaxLength="{Binding ElementName=LabelWithInputUC,Path=MaxLength}"
                     IsReadOnly="{Binding ElementName=LabelWithInputUC,Path=IsReadOnly}"
                     Text="{Binding ElementName=LabelWithInputUC,Path=Text,UpdateSourceTrigger=PropertyChanged}"/>            

    </Grid>
</UserControl>

Label, MaxLength, IsReadOnly and Text are dependency properties in my usercontrol. The window that uses my control has the text property bound to a property in the viewmodel and there it get's validated with INotifyDataErrorInfo.

If I try to set the textbox as the controls ValidationAdornerSite I don't see any validation errors anymore. How do I use the inner textbox's error template for my user control?

Update

I created an minimal example to show the problem.

Marcel L
  • 1
  • 1
  • Set the `Validation.Error` template attached property to `{StaticResource MaterialDesignValidationErrorTemplate}` or whatever template you want to use? – mm8 Mar 08 '23 at 13:41
  • The textbox already has the right ErrorTemplate. But the validation error occurs on the parents (the usercontrols) binding instead of the textboxes. Can I somehow bind the textboxes validation errors to the usercontrols errors so the textbox knows about it? – Marcel L Mar 08 '23 at 13:56
  • No but you can use the `Validation.MarkInvalid` method to mark it as invalid: https://stackoverflow.com/a/4287354/7252182 – mm8 Mar 08 '23 at 13:58
  • Not really sure if that helps me. That only makes me change my validation from nice INotifyDataErrorInfo mvvm-style validation to validation in code behind. And I still have to validate in my viewmodel because I need the results of that validation there. There surely have to be a way to bubble this validation from one binding (the usercontrols text binding) to another one (the internal textboxes text binding). I can't use ValidationAdornerSite or SiteFor for that? Sounds like it would be the right approach. – Marcel L Mar 08 '23 at 14:29
  • If you want to handle this using MVVM, you should perform the validation on the source properties that are bound to the textboxes obviously, i.e. the control should invalidate the `Text` property in your example. – mm8 Mar 08 '23 at 14:30
  • Do I have a general problem with my code then? The textbox is bound to a dependencyproperty as you see above and my control does not have a viewmodel. The validation should happen in the viewModel of my window that holds that control. – Marcel L Mar 08 '23 at 14:39
  • The textbox is bound to a dependency property of the control which in turn should be bound to the view model property being validated. – mm8 Mar 08 '23 at 14:43
  • That's exactly the case. But that only raises the error on the outer binding, not the inner binding between dependency property and textbox. I just found another so post about that, but nobody came up with a solution there. https://stackoverflow.com/questions/55534983/wpf-validation-error-style-in-reuseable-usercontrol – Marcel L Mar 08 '23 at 14:46
  • So why can't you use `Validation.MarkInvalid` in the control then? – mm8 Mar 08 '23 at 14:48
  • I wouldn't even know in the code behind that my validation changed. Or did I overlook something? I put together an minimal example project under https://github.com/ml-spreetropol/WPFValidationProblem . I would like to use the textboxes error template, so that red border should be around that second textbox only. How would you do that? Thank you! – Marcel L Mar 08 '23 at 15:22

0 Answers0