允许通过绑定TabItem的 [英] Enable a TabItem via Binding
问题描述
我想在一个应用程序,不同的页面的TabItems使用MVVM。
I want to use MVVM in an application where the different pages are TabItems.
为此,我使用一个观察的集合我的视图模型(项目),并将其绑定到的ItemSource的TabControls
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>
我想现在要做的就是从包含集合视图模型内启用/禁用标签。视图模型的基类包含一个依赖属性IsEnabled,我想这个绑定的意见。如果我直接在这样的观点做到这一点:
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}"
当我打开IsEnabled属性设置为false标签页的内容被禁用。但我真正想要的是也是禁用的TabPage的标签,而不仅仅是内容。
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.
感谢您的帮助!
推荐答案
也许你可以尝试这样的事情 -
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>
这篇关于允许通过绑定TabItem的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!