WPF DataTrigger on TabItem

本文关键字:TabItem on DataTrigger WPF | 更新日期: 2023-09-27 18:21:36

我有以下UserControl:

<UserControl x:Class="WpfExample.Views.TabsUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:v="clr-namespace:WpfExample.Views"
             xmlns:vm="clr-namespace:WpfExample.ViewModels"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             DataContext="{Binding TabsViewModel, Source={StaticResource Locator}}">
    <TabControl ItemsSource="{Binding Tabs}"
                SelectedItem="{Binding SelectedTabViewModel}">
        <TabControl.Resources>
            <DataTemplate DataType="{x:Type vm:ViewDatabaseTableViewModel}">
                <v:ViewDatabaseTableUserControl />
            </DataTemplate>
            <DataTemplate DataType="{x:Type vm:CustomerViewModel}">
                <v:CustomerView />
            </DataTemplate>
            <DataTemplate DataType="{x:Type vm:SystemSetupViewModel}">
                <v:SystemSetupUserControl />
            </DataTemplate>
        </TabControl.Resources>
        <TabControl.ItemContainerStyle>
            <Style TargetType="TabItem">
                <Setter Property="Header" Value="{Binding Header}" />
                <Setter Property="Width" Value="120" />
                <!--<Setter Property="IsEnabled" Value="False" />
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.ProductIsLicensed}" />
                    <Trigger Property="{Binding Header}" Value="System Setup">
                        <Setter Property="IsEnabled" Value="True" />
                    </Trigger>
                </Style.Triggers>-->
            </Style>
        </TabControl.ItemContainerStyle>
    </TabControl>    
</UserControl>

我想使用DataTrigger根据产品是否获得许可来启用/禁用选项卡。我认为这可能有效(添加在<TabControl>块中):

  <ControlTemplate TargetType="TabItem">
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.ProductIsLicensed}" Value="False">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

但它给了我一个例外,"PresentationFramework.dll中发生了类型为"System.Windows.Markup.XamlParseException"的首次机会异常"。

如果ProductIsLicensed值为False,有人能告诉我如何完成选项卡的禁用吗?

WPF DataTrigger on TabItem

不需要触发器或ControlTemplate,这只会破坏所有默认样式。只需创建一个样式并直接绑定IsEnabled

<Style TargetType="TabItem">
    <Setter Property="IsEnabled" Path="{Binding IsProductLicensed}"/>
</Style>

通过不为样式提供键,它将应用于用户控件中的所有TabItems。

ControlTemplate定义控件的所有外观,并且只能应用于样式内的Template属性。只有当您想从头开始对控件进行完全样式化时,才应该使用它。就是这样

<Style TargetType="TabItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TabItem">
                <!-- Define ALL here - look, effects, triggers etc. -->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>