最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to pass INotifyDataErrorInfo Errors into nested user controls in WPF? - Stack Overflow

programmeradmin4浏览0评论

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}}"/>

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}}"/>
Share Improve this question asked yesterday mauriziopatinomauriziopatino 495 bronze badges 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 – Nawed Nabi Zada Commented 20 hours ago
Add a comment  | 

2 Answers 2

Reset to default 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}}"/>/>

Passing INotifyDataErrorInfo Errors into Nested User Controls in WPF

To pass INotifyDataErrorInfo validation errors into nested user controls in WPF, ensure that your ViewModel implements INotifyDataErrorInfo and binds to the nested control’s DataContext. Use Binding.Validation.Errors in XAML or propagate errors via Validation.Errors in DependencyProperties.

Example:

Implement INotifyDataErrorInfo in the parent ViewModel. Bind DataContext of the nested UserControl to the relevant property. Use ValidatesOnNotifyDataErrors=True in bindings. If issues persist, consider Command Binding or explicit error propagation. Let me know if you need more details!

发布评论

评论列表(0)

  1. 暂无评论