自定义TabItem样式和标题绑定

时间:2014-04-30 14:25:02

标签: c# wpf xaml styles wpf-controls

我在模板中为我的TabItem创建了一个样式,我添加了一个Label,并将Content属性绑定到TabItem的Header属性。标签具有Foreground属性,该属性在IsMouseOveror Selected上更改。

    <Style TargetType="{x:Type TabItem}" >
        <Setter Property="Template" >
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Grid x:Name="templateRoot" SnapsToDevicePixels="True">
                        <Border x:Name="mainBorder" Margin="0">
                            <Border x:Name="innerBorder" BorderBrush="LightGray" BorderThickness="0,0,0,1" Margin="5,0,5,0" />
                        </Border>
                        <Label x:Name="txtHeader"  Content="{TemplateBinding Header}" Padding="0" Focusable="False" FontSize="16" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"  />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                            </MultiDataTrigger.Conditions>
                            <Setter Property="Foreground" TargetName="txtHeader" Value="#FF1577D8"/>
                        </MultiDataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="true"/>
                            </MultiDataTrigger.Conditions>
                            <Setter Property="Foreground" TargetName="txtHeader" Value="#FF1577D8"/>
                        </MultiDataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

在窗口中:

<TabControl>
        <TabItem >
            <TabItem.Header>
                <TextBlock Text="Tab1" Foreground="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Label}}}"  ></Label>
            </TabItem.Header>
        </TabItem>
        <TabItem Header="Tab2" />
 </TabControl>

Tab1不起作用,但tab2工作正常 我试过在Path = Foreground和(Label.Foreground)绑定中也尝试过Templatebinding,... 如果我尝试一个新的应用程序,它一切都很完美,可能是什么原因?

0 个答案:

没有答案