TabItem上的WPF DataTrigger

时间:2014-09-10 20:48:08

标签: c# wpf tabcontrol datatrigger tabitem

我有以下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>

但它给了我一个例外&#34;类型&#39; System.Windows.Markup.XamlParseException&#39;的第一次机会异常。发生在PresentationFramework.dll&#34;。

如果ProductIsLicensed值为False,有人可以告诉我如何完成标签的停用吗?

1 个答案:

答案 0 :(得分:0)

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

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

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

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>