Enable a TabItem via Binding

核能气质少年 提交于 2019-12-21 03:55: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!


回答1:


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>


来源:https://stackoverflow.com/questions/9291394/enable-a-tabitem-via-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!