Question

I'm currently in the need of setting the SelectedIndex property of my TabControl when a certain event (IEventAggregator) takes place and thought about how I'd implement that.

I came up with 2 possibilities:

  • Use GetView() provided by ViewAware in order to access my TabControl and set the SelectedIndex to my value
  • Use a property in my associated ViewModel and bind this property to my TabControl's SelectedIndex property via XAML

Both options are working fine but I personally want to get this question answered since this is not the first time I'm wondering where to implement the functionality in such cases.

I know that the first option won't enable the Notify support but besides that: What would be the proper way?

Was it helpful?

Solution

Having a GetView() method to manipulate the view directly from the viewmodel completely breaks MVVM. You might as well just put all your logic in codebehind. The whole point of MVVM is to abstract away the actual view so that it is decoupled from the logic, and the app can be unit tested.

What if you change your mind about the tabs in the future and decide to show your multiple views some other way? You've now got to start editing your viewmodel to edit the new view instead of just tweaking some XAML.

And for unit testing you're going to have no way to mock out your TabControl.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top