通过绑定启用 TabItem
我想在不同页面是 TabItem 的应用程序中使用 MVVM。
为此,我使用视图模型(项目)的可观察集合并将其绑定到选项卡控件 ItemSource。
对于每个视图模型,我创建了一个单独的数据模板来呈现正确的视图,如下所示:
<DataTemplate DataType="{x:Type baseVm:AViewModel}">
<baseVw:AView />
</DataTemplate>
为了在选项卡标题中显示正确的名称,我创建了另一个数据模板以应用于每个选项卡控件的元素:
<DataTemplate x:Key="ViewModelTabTemplate">
<DockPanel>
<ContentPresenter Content="{Binding Path=Name}"/>
</DockPanel>
</DataTemplate>
选项卡控件如下所示:
<TabControl x:Name="myTabControl"
ItemsSource="{Binding Items}"
ItemTemplate="{DynamicResource ViewModelTabTemplate}">
</TabControl>
我现在想要做的是从包含集合的视图模型中启用/禁用选项卡。视图模型的基类包含一个依赖属性 IsEnabled,我想将其绑定到视图。如果我直接在视图中执行此操作,如下所示:
IsEnabled="{Binding IsEnabled, FallbackValue=true}"
当我将 IsEnabled 属性设置为 false 时,选项卡页的内容将被禁用。但我真正想要的是禁用选项卡页面的选项卡,而不仅仅是内容。
感谢您的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许你可以尝试这样的事情 -
Maybe you could try something like this -