0

我在我的表单中加入样式和代码时遇到了麻烦:

这是我的情况:

我的 TabItem 样式:

   <Style TargetType="TabItem" x:Key="testStyle">
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="Padding" Value="6,2,6,2" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Stretch" />
        <Setter Property="MinWidth" Value="5" />
        <Setter Property="MinHeight" Value="5" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TabItem">
                    <DockPanel Width="120" x:Name="rootPanel">
                        <ContentPresenter ContentSource="Header"  RecognizesAccessKey="True" />
                        <Image x:Name="rootImage"/>
                        <Label x:Name="rootLabel" FontSize="18" />
                    </DockPanel>
                    <ControlTemplate.Triggers>

这是我应用我的风格的地方

            <TabItem Style="{StaticResource testStyle}">
                <TabItem.Header>

                </TabItem.Header>

但是:如何将值设置为名为rootImageand的图像和标签rootLabel

4

2 回答 2

1

就像您为 your 所做的那样TabItem,您可以为您的Image和指定样式Label-

<Image x:Name="rootImage" Style="{StaticResource ImageStyle}"/>
<Label x:Name="rootLabel" FontSize="18" Style="{StaticResource LabelStyle}" />

如果您想更改Header外观,则需要覆盖HeaderTemplate而不是全部Template-

        <TabItem.HeaderTemplate>
            <DataTemplate>
                <StackPanel>
                    <Image x:Name="rootImage"
                           Style="{StaticResource ImageStyle}"/>
                    <Label x:Name="rootLabel" FontSize="18"
                           Style="{StaticResource LabelStyle}"/>
                </StackPanel>
            </DataTemplate>
        </TabItem.HeaderTemplate>
于 2013-03-27T09:55:32.010 回答
1

我假设你有一个名为SomeClass

public class SomeClass
{
    public string SomeLabel;
    public string SomeImage;
}

现在改变你的风格

<DockPanel Width="120" x:Name="rootPanel">
    <ContentPresenter ContentSource="Header"  RecognizesAccessKey="True" />
    <Image x:Name="rootImage" Source={Binding SomeImage}/>
    <Label x:Name="rootLabel" Content={Binding SomeLabel} FontSize="18" />
</DockPanel>

最后:

<TabItem Style="{StaticResource testStyle}" Name="myTabItem">
    <TabItem.Header>

    </TabItem.Header>
</TabItem>

在后面的代码中:

myTabItem.DataContext = new SomeClass(); //create a SomeClass with proper label and image
于 2013-03-27T10:06:56.923 回答