1

I am using MVVM pattern, my application is basically TabControl with 3 TabItems with DataGrids inside them.

I can save all data from grids to csv files and load it back. I try to add functionality that after user clicks button, a new window of string collection with all DataGrids validation errors pops out. I almost succeeded but I find out that I have to click through all TabItems to fire up their IDateErrorInfo property validation and gather the strings.

So my question is how can I manually fire the validation of specific tab from code? I tried something like jump through all tabs with SelectedIndex property of TabControl in loop firing PropertyChanged("SelectedIndex") each time and it changes the tab but doesn't fire validation and I get errors only from last, now visible tab.

EDIT

More details:

My every tab has its own ViewModel with ObservableCollection<[TabModel]> connected to DataGrid. All 3 TabModels have implemented IDataErrorInfo interface. Every tab has own xaml file and all these files are gathered in MainWindows.xaml's TabControl like this:

<TabControl SelectedIndex="{Binding SelectedTab, UpdateSourceTrigger=PropertyChanged}">

        <TabItem Width="auto" Header="FOO" Name="Foo">
            <Controls:Foo DataContext="{Binding FooVM}"/>
        </TabItem>
        ...
</TabControl>

In Foo.xaml I have simple DataGrid with columns and validation turned on on specific column, i.e.:

<DataGridTextColumn Header="ID" Binding="{Binding Id, ValidatesOnDataErrors=True}"/>

In every TabModel I have List<string> Errors to which I add every result from IDataErrorInfo public string this[string name]

I try to get/do all validation from MainWindowVM's code position.

  • Oh yes, didn't write this, thought it's not that important and doesn't affect the solution to this problem :x In every TabModel I have List Errors to which I add every result from IDataErrorInfo `public string this[string name]` – Adam Dobrzeniewski Jan 12 '20 at 20:59
  • Have a look here - https://stackoverflow.com/questions/14023552/how-to-use-idataerrorinfo-error-in-a-wpf-program – Sasha Jan 13 '20 at 08:02
  • It's not very helpful. I applied tooltip style for all my grids, the problem is to trigger validation in the first place so I can see those `(Validation.Errors)[0].ErrorContent` from another tab. – Adam Dobrzeniewski Jan 14 '20 at 12:23
  • What if you store validation error in VM? Then you don't need to rely on the UI being refreshed. Binding kicks in when UI controls are shown. If you set ItemsSource on TabControl, you won't need to set the data Context on the control. – XAMlMAX Jan 14 '20 at 15:51
  • Sorry but I really don't get you. What do you mean by "store validation error in VM"? You mean the MainWindowVM or VMs of specific tabs? "Binding kicks in when UI controls are shown"? That's my main goal in this whole thread :x So it's not that simple for me. – Adam Dobrzeniewski Jan 17 '20 at 09:55

0 Answers0