1

I have a WPF application where I have 2 custom UserControl, both have the characteristic that are reusable. The first user control is an FormGroup where exposes a label and a reference to the other user control. The second UserControl is a custom PlaceholderTextbox where exposes an TextBox and TextBlock in simple terms.

- MainWindow.xaml

<FormGroupUserControl Text={Binding Text} />

- FormGroupUserControl

<Label />
<PlaceholderTextBoxUserControl Text="{Binding Text, RelativeSource={RelativeSource AncestorType=UserControl}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

- PlaceholderTextBoxUserControl

<TextBlock />
<TextBox Text="{Binding Placeholder, RelativeSource={RelativeSource AncestorType=UserControl}}"/>

So basically I’m propagating the Text binding into the TextBox, the issue I’m having is when trying to make validations with INotifyDataErrorInfo that is defined in my MainViewModel is that the red border is outlining my entire FormGroupUserControl, I know that this is how should work because I’m adding errors into the entire UserControl, but how can I propagate that Validation error into the TextBox inside PlaceHolderTextBoxUserControl.

I tried with ValidationAdornerSiteFor and the red border now is in the entire PlaceHolderTextBoxUserControl UserControl, but not in the TextBox. Anyways this can be a solution but how can I edit the style of the Validation red error border, otherwise how can I propagate the validation error into the TextBox

- FormGroupUserControl

<Label />
<PlaceholderTextBoxUserControl Text="{Binding Text, RelativeSource={RelativeSource AncestorType=UserControl}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Validation.ValidationAdornerSiteFor="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"/>
1
  • 1
    Just bind the Text property TextBox in PlaceholderTextBoxUserControl to the Text in View Model, if those controls are nested into each other they all will have the same view model as DataContext, unless you set the data context for those control, which is not recommended Commented Feb 19 at 10:12

1 Answer 1

0

I'm not where I can test this. So here's something I'd try but I don't actually know if it'll work.

I'm willing to bet that using a second Validation.ValidationAdornerSiteFor on the TextBox on PlaceholderTextBoxUserControl will drill that notification down further. To be clear, that's in addition to the one you put at the end of your question on FormGroupUserControl. I'm betting that WPF reads those sequentially and drops the notification down a level and then down another and that it coordinates those the way I'd expect.

<TextBox Text="{Binding Text, RelativeSource={RelativeSource AncestorType=UserControl}}"
Validation.ValidationAdornerSiteFor="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"/>/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.