Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable a TabItem via Binding

I want to use MVVM in an application where the different pages are TabItems.

For this I use an observable collection of my view models (Items) and bind it to the tabcontrols ItemSource.

For each view model, I created an individual data template to render the correct view like this:

<DataTemplate DataType="{x:Type baseVm:AViewModel}">
  <baseVw:AView /> 
</DataTemplate>

To display the correct name in the tab's header I created another data template to be applied to each of the tab control's elements:

<DataTemplate x:Key="ViewModelTabTemplate">
  <DockPanel>
    <ContentPresenter Content="{Binding Path=Name}"/>
  </DockPanel>
</DataTemplate>

The tab control looks like this:

<TabControl x:Name="myTabControl" 
            ItemsSource="{Binding Items}" 
            ItemTemplate="{DynamicResource ViewModelTabTemplate}">
</TabControl>

What I want to do now is to enable/disable tabs from within the view model that contains the collection. The view model's base class contains a dependency property IsEnabled and I would like to bind this to the views. If I do this directly in the view like this:

IsEnabled="{Binding IsEnabled, FallbackValue=true}"

the tab page's content gets disabled when I turn the IsEnabled property to false. But what I really want is to also disable the tabpage's tab and not just the content.

Thanks for any help!

like image 799
tabina Avatar asked Feb 15 '12 10:02

tabina


1 Answers

Maybe you could try something like this -

<TabControl>
    <TabControl.ItemContainerStyle>
        <Style TargetType="{x:Type TabItem}">
             <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>        
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>
like image 52
Dror Avatar answered Sep 19 '22 20:09

Dror