0

I am currently running into an issue where I want all of my tabs to be immediately bound to my ViewModel. For some reason, it seems that WPF doesn't bind my other TabItem's until I select them for the first time. I am eager for a solution in this because I have validation on those others tabs. Until the binding occurs on those other tabs, my app thinks everything is valid when it isnt. If it helps, I am using FluentValidation for my validation.

I have tried using someone's TabControlEx to see if that would help me but it doesn't. I also tried to cycle through all the tabs after I load the data to force the binding but that didn't always work on slower devices. I am not a fan of this solution either.

Community
  • 1
  • 1
Travyguy9
  • 4,774
  • 8
  • 43
  • 63
  • Questions seeking debugging help (**"why isn't this code working?"**) must include the desired behavior, a **specific problem or error** and **the shortest code necessary** to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve) – Federico Berasategui Aug 24 '15 at 16:14

1 Answers1

2

I don't know why you are being voted down, seems like a valid question, but you will still get a vague answer, due to the nature of the TabControl:

The default style for WPF TabControl only contains a single ContentControl which is used for displaying all tabs. Thus, the visual tree representing a tabs content is created on demand; and tears down to be replaced with a new visual tree when the content/tab switches.

The common issue here, is that switching between tabs becomes slow, and thus there are a couple of solutions out there:

However, what happens with the solutions is that ContentPresenters are cached...but they still wont be created until a user first hits the tab. Thus, you will still have the same issue.

Thus you need some sort of architectural change. You need your set of view models to be validated from a trigger in code, on initial startup. Waiting for the tab control to render items just isn't going to work for you.

You probably need to bubble the error up in your view model hierarchy to display in the TabItem.Header or in the window itself; perhaps you might have a MainWindowViewModel where you can display a top level error.

Community
  • 1
  • 1
James Willock
  • 1,999
  • 14
  • 17