views:

1129

answers:

1

I am developing an mvvm app with wpf. A requirement just got added on to block the user from changing tabs if a textbox has text.

What is the best way to do this completely in the viewmodel? I don't know how to block a tabitem because there is no dependencyobject command in the tabcontrol to tie into, do i need to roll my own tabcontrol and build an ICommand around the SelectionChanged event?

Should I simply (eegad..don't say it) put code in the code behind of the view in the SelectionChanged event?

Do I have an alternative that I haven't thought of?

+4  A: 

You might consider binding each of the TabItems' IsEnabled property to a property in your ViewModel (e.g. ViewModel.TabsEnabled) and set that property to False when the textbox has text. That way, you'll be able to enable/disable those tabs from your ViewModel without having to have a code behind file for that particular view.

Which means you'll have something like the following in your view (assuming your ViewModel is a static class named ViewModel):

<TabItem IsEnabled="{Binding Source={x:Static local:ViewModel.TabsEnabled}}"/>

Then you just have to set the TabsEnabled property on the ViewModel when one of the textboxes has content; there are a couple of ways to do this, but if they are bound to your ViewModel you should have plenty of opportunities to listen for changes and set TabsEnabled as appropriate.

Nicholas Armstrong
From what I've read it's more correct to use something like *IsEnabled* instead of *TabsEnabled*, as *Tabs* assumes knowledge of the view (from the view model).
Si