I have the following 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>
I would like to use a DataTrigger to enable/disable tabs based on wheter or not the product is licensed. I thought this might work (added within the <TabControl>
block):
<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>
But it gives me an exception of "A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll".
Can someone tell me how to accomplish the disabling of tabs if the ProductIsLicensed
value is False?
No need for triggers or ControlTemplate, this will just ruin all the default stylings. Just create a style and bind the IsEnabled directly
<Style TargetType="TabItem">
<Setter Property="IsEnabled" Path="{Binding IsProductLicensed}"/>
</Style>
By not supplying a key to the style, it will apply to all TabItems in the user control.
A ControlTemplate defines all the look and feel of the control, and can only be applied to the Template property inside a style. It should only be used if you want to completely style a control from scratch. This is how it would look
<Style TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<!-- Define ALL here - look, effects, triggers etc. -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>