2014-09-10 182 views
0

我有以下的用户控件:WPF DataTrigger上的TabItem

<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启用/基于wheter或没有在产品获得许可禁用标签。我想这可能工作(在<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> 

但它给我“型‘System.Windows.Markup.XamlParseException’发生在PresentationFramework.dll的第一个机会异常”的一个例外。

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

+0

你把ControlTemplate放在一个样式里面吗? – sondergard 2014-09-10 20:59:29

回答

0

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

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

绑定的IsEnabled通过不提供一键样式,它将适用于用户控制所有的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>