关于TabControl的Trigger【项目】

我有一个TabControl

<TabControl x:Name="ToolSystemSection"

                            Grid.Row="4"

                            ContentTemplate="{StaticResource WaterSwellHTToolSystemContent_Template}"

                            IsSynchronizedWithCurrentItem="True"

                            ItemContainerStyle="{StaticResource WaterSwellHTToolSystemItemContainer_Template}"

                            ItemsSource="{Binding Path=ToolInfoCollection}"

                            Style="{StaticResource ToolSystemTabControl_Style}">

                    <TabControl.Background>

                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">

                            <GradientStop Offset="1" Color="#FF6C6C6F" />

                            <GradientStop Color="#FFA8AAAD" />

                        </LinearGradientBrush>

                    </TabControl.Background>

                </TabControl>
View Code

 

要实现上面的Behivor需要设计其trigger。

<Style x:Key="WaterSwellHTToolSystemItemContainer_Template" TargetType="{x:Type TabItem}">

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="{x:Type TabItem}">

                    <Border x:Name="border"

                            Height="35"

                            MinWidth="180"

                            Background="#808284"

                            BorderThickness="1,1,3,0"

                            CornerRadius="3,3,0,0">

                        <Grid Background="Transparent">

                            <Grid.ColumnDefinitions>

                                <ColumnDefinition Width="140" />

                                <ColumnDefinition Width="30" />

                            </Grid.ColumnDefinitions>

                            <TextBlock HorizontalAlignment="Center"

                                       VerticalAlignment="Center"

                                       FontSize="11pt"

                                       Style="{StaticResource Univers57_Condensed}"

                                       Text="{Binding DesignName,

                                                      UpdateSourceTrigger=PropertyChanged}" />

                            <Button x:Name="CloseButton"

                                    Grid.Column="1"

                                    HorizontalAlignment="Center"

                                    Command="{Binding DeleteDesignCommand}"

                                    Style="{StaticResource Cross_Button_Style}"

                                    Visibility="Collapsed" />

                        </Grid>

                    </Border>

                    <ControlTemplate.Triggers>





                        <Trigger Property="IsSelected" Value="True">

                            <Setter TargetName="CloseButton" Property="Visibility" Value="Visible" />

                            <Setter TargetName="border" Property="Background" Value="#FFA7A9AB" />

                        </Trigger>



                        <MultiDataTrigger>

                            <MultiDataTrigger.Conditions>

                                <Condition Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}" Value="True" />

                                <Condition Binding="{Binding Path=Items.Count, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" Value="1" />

                            </MultiDataTrigger.Conditions>

                            <Setter TargetName="CloseButton" Property="Visibility" Value="Collapsed" />

                        </MultiDataTrigger>



                        <!--

                            <Trigger Property="IsSelected" Value="True">

                            <Setter TargetName="CloseButton" Property="Visibility" Value="Visible" />

                            <Setter TargetName="border" Property="Background" Value="#FFA7A9AB" />

                            </Trigger>

                            <DataTrigger Binding="{Binding Path=Items.Count, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" Value="1">

                            <Setter TargetName="CloseButton" Property="Visibility" Value="Collapsed" />

                            </DataTrigger>

                        -->



                    </ControlTemplate.Triggers>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

</ResourceDictionary>

方法一:设置一个trigger和一个MultiTrigger,其实<Condition Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}" Value="True" />不用写

方法二:设置一个Trigger和一个DataTrigger

说明的三点问题:

  1. MultiDataTrigger里只能有Binding模式的Trigger,所以叫dataTrigger。但是需要用到Control itself自己的一个属性Path=IsSelected,要写成Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}的模式,对比其在普通trigger下的表现实行<Trigger Property="IsSelected" Value="True">
  2. <Condition Binding="{Binding Path=Items.Count, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" Value="1" />属性Items.Count是<TabControl>里的属性,所以这里写成AncestorType={x:Type TabControl}而不是TemplateParent
  3. <ControlTemplate.Triggers>......</ControlTemplate.Triggers>可以同时包含多种混合的Trigger

你可能感兴趣的:(trigger)