Enable a TabItem via Binding
Asked Answered
O

1

10

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!

Orphanage answered 15/2, 2012 at 10:11 Comment(0)
M
22

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>
Miscalculate answered 15/2, 2012 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.