通过绑定启用 TabItem

发布于 2025-01-06 12:34:05 字数 999 浏览 5 评论 0原文

我想在不同页面是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫儰鉨ミ守葔 2025-01-13 12:34:05

也许你可以尝试这样的事情 -

<TabControl>
    <TabControl.ItemContainerStyle>
        <Style TargetType="{x:Type TabItem}">
             <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>        
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文