【问题标题】:On Tabitem selection, Style is not performing在 Tabitem 选择时,样式未执行
【发布时间】:2017-10-17 19:22:23
【问题描述】:

我在 xaml 文件中为 Tab Control 和 TabItem 创建了样式。

当 TabControl 第一次在 window 上加载时,它会根据需要应用样式。 但是当我选择另一个 tabItem 时,它没有执行 IsSelection 属性样式。

选择时 : Path Stroke="blue" and background of Bd="yellow".

请帮我解决这个问题。

提前谢谢你。

<Style x:Key="SimpleTabItemStyle" TargetType="{x:Type TabControl}">
        <Setter Property="Padding" Value="4,4,4,0"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="BorderBrush" Value="{DynamicResource WhiteColorBrush}"/>
        <Setter Property="Background" Value="{DynamicResource NormalBrush}"/>

        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabControl}">
                    <Grid ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition x:Name="ColumnDefinition0" Width="480"/>
                            <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition x:Name="RowDefinition0" Height="30"/>
                            <RowDefinition x:Name="RowDefinition1" Height="*"/>
                        </Grid.RowDefinitions>

                        <TabPanel x:Name="HeaderPanel" IsItemsHost="true" Margin="2,2,2,0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
                        <Border x:Name="ContentPanel" 
                            CornerRadius="0,5,5,5" 
                                BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{DynamicResource NormalBrush}" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">

                            <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource WhiteColorBrush}"/>
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="MediumTabItemStyle" TargetType="{x:Type TabItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="Padding" Value="3,1"/>
        <Setter Property="Focusable" Value="True"/>
        <Setter Property="Width" Value="105px"/>
        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}" >
                    <Grid SnapsToDevicePixels="true" Height="32" Focusable="False">
                        <Path x:Name="Bd" Fill="{DynamicResource NormalBrush}" StrokeThickness="2" Stroke="Blue" Data="M32.112576,21.370581 C32.11147,19.654586 32.018481,10.83178 32.130291,
                          2.8647957 32.168301,0.15634975 32.890504,-0.063229885 33.673263,-0.10401306 33.673263,-0.10401306 46.603605,-0.10439528 46.603605,-0.10439528 48.464699,-0.081679943 48.572192,13.549447 50.678186,21.503142" Stretch="Fill" Width="105px" Cursor="Hand" />
                        <TextBlock x:Name="Content" Padding="1" TextWrapping="WrapWithOverflow" Width="95" Foreground="{DynamicResource BlackColorBrush}" HorizontalAlignment="Left" TextBlock.TextAlignment="Center" VerticalAlignment="center"
                               Text="{Binding Header, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}" ToolTip="{Binding Header, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}" TextTrimming="CharacterEllipsis"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="False">
                            <Setter Property="ToolTip" TargetName="Content" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem},Mode=FindAncestor},Path=Header}"/>
                            <Setter Property="Fill" TargetName="Bd" Value="{DynamicResource UnSelectedBrush}"/>
                            <!--<Setter Property="TextTrimming" TargetName="Content" Value="CharacterEllipsis"/>-->
                            <Setter Property="Panel.ZIndex" Value="0"/>
                            <Setter Property="Stroke" TargetName="Bd" Value="{DynamicResource WhiteColorBrush}"/>
                            <Setter Property="Margin" Value="0,0,0,-1" TargetName="Bd"/>

                        </Trigger>

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

                            <Setter TargetName="Bd" Property="Opacity" Value="0.6"/>
                            <Setter Property="Cursor" TargetName="Bd" Value="Hand" />
                        </Trigger>

                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Margin" Value="0,0,0,-3" TargetName="Bd"/>
                            <Setter Property="Fill" TargetName="Bd" Value="{DynamicResource NormalBrush}"/>
                            <Setter Property="Stroke" TargetName="Bd" Value="Yellow"/>
                            <Setter Property="StrokeThickness" TargetName="Bd" Value="2"/>
                            <!--<Setter Property="TextBlock.FontWeight" TargetName="Content" Value="DemiBold"/>-->
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="false"/>
                                <Condition Property="IsMouseOver" Value="true"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Margin" Value="-1" TargetName="Bd"/>
                            <Setter Property="Fill" TargetName="Bd" Value="{DynamicResource UnSelectedItemBrush}"/>
                            <Setter Property="Cursor" TargetName="Bd" Value="Hand"/>
                            <!--<Setter Property="TextBlock.FontWeight" TargetName="Content" Value="DemiBold"/>-->
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Fill" TargetName="Bd" Value="{DynamicResource DisabledBackgroundBrush}"/>
                            <Setter Property="Foreground" TargetName="Content" Value="{DynamicResource WhiteColorBrush}"/>
                            <Setter Property="Cursor" TargetName="Bd" Value="Hand"/>
                            <Setter Property="Margin" Value="0,0,0,-3" TargetName="Bd"/>
                            <Setter Property="Fill" TargetName="Bd" Value="{DynamicResource NormalBrush}"/>
                            <Setter Property="Stroke" TargetName="Bd" Value="Yellow"/>
                            <Setter Property="StrokeThickness" TargetName="Bd" Value="2"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

【问题讨论】:

  • 那么,你想实现:当item被选中时,Stroke为“蓝色”,背景为“黄色”?
  • 是的,选择时触发器未执行@Iron
  • 你的NormalBrush好吗?
  • @iron 它是浅灰色的
  • 在触发器Trigger Property="IsSelected" 中,您已将Stroke 设置为Yellow,将Fill (Background) 设置为NormalBrush,而不是“蓝色”和“黄色”。

标签: c# wpf xaml


【解决方案1】:

试试下面的代码:

<ControlTemplate.Triggers>
    <Trigger Property="IsSelected" Value="False">
        <Setter Property="ToolTip" TargetName="Content" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem},Mode=FindAncestor},Path=Header}"/>
        <Setter Property="Fill" TargetName="Bd" Value="{DynamicResource UnSelectedBrush}"/>
        <!--<Setter Property="TextTrimming" TargetName="Content" Value="CharacterEllipsis"/>-->
        <Setter Property="Panel.ZIndex" Value="0"/>
        <Setter Property="Stroke" TargetName="Bd" Value="{DynamicResource WhiteColorBrush}"/>
        <Setter Property="Margin" Value="0,0,0,-1" TargetName="Bd"/>

    </Trigger>

    <Trigger Property="IsMouseOver" Value="True">
        <!--<Setter TargetName="Bd" Property="Opacity" Value="0.6"/>-->
        <Setter Property="Cursor" TargetName="Bd" Value="Hand" />
    </Trigger>

    <Trigger Property="IsSelected" Value="True">
        <Setter Property="Margin" Value="0,0,0,-3" TargetName="Bd"/>
        <Setter Property="Fill" TargetName="Bd" Value="Blue"/>
        <Setter Property="Stroke" TargetName="Bd" Value="Yellow"/>
        <Setter Property="StrokeThickness" TargetName="Bd" Value="2"/>
        <!--<Setter Property="TextBlock.FontWeight" TargetName="Content" Value="DemiBold"/>-->
    </Trigger>
    <!--<MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="IsSelected" Value="false"/>
            <Condition Property="IsMouseOver" Value="true"/>
        </MultiTrigger.Conditions>
        <Setter Property="Margin" Value="-1" TargetName="Bd"/>
        <Setter Property="Fill" TargetName="Bd" Value="{DynamicResource UnSelectedItemBrush}"/>
        <Setter Property="Cursor" TargetName="Bd" Value="Hand"/>
        --><!--<Setter Property="TextBlock.FontWeight" TargetName="Content" Value="DemiBold"/>--><!--
    </MultiTrigger>-->
    <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Fill" TargetName="Bd" Value="{DynamicResource DisabledBackgroundBrush}"/>
        <Setter Property="Foreground" TargetName="Content" Value="{DynamicResource WhiteColorBrush}"/>
        <Setter Property="Cursor" TargetName="Bd" Value="Hand"/>
        <Setter Property="Margin" Value="0,0,0,-3" TargetName="Bd"/>
        <!--<Setter Property="Fill" TargetName="Bd" Value="{DynamicResource NormalBrush}"/>
        <Setter Property="Stroke" TargetName="Bd" Value="Yellow"/>-->
        <Setter Property="StrokeThickness" TargetName="Bd" Value="2"/>
    </Trigger>
</ControlTemplate.Triggers>

【讨论】:

  • 不,它不工作。当我选择另一个选项卡项时,它不会触发“IsSelection”属性。
  • IsSelection 属性是什么?
  • 对不起 IsSelected 属性,我在其中定义背景和笔画值
  • 你能显示绑定TabItemIsSelected的代码吗?
  • 我发现了我的错误。我装错了。我已经改正了。感谢您的指导。
猜你喜欢
  • 2012-08-04
  • 2020-11-28
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 2019-07-30
相关资源
最近更新 更多