ghiboz
ghiboz

Reputation: 8003

wpf use elements declared in style

i'm in trouble joining styles and code in my form:

here is my situation:

my TabItem style:

   <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>

and here is where I apply my style

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

                </TabItem.Header>

but: how can I set the values to my Image and label called rootImage and rootLabel?

Upvotes: 0

Views: 96

Answers (2)

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32501

I assume you have a class named SomeClass

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

Now change your style

<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>

Finally:

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

    </TabItem.Header>
</TabItem>

And in the code behind:

myTabItem.DataContext = new SomeClass(); //create a SomeClass with proper label and image

Upvotes: 1

Rohit Vats
Rohit Vats

Reputation: 81253

Just like you did for your TabItem, you can have the style specified for your Image and Label -

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

In case, you want to change the Header look, you need to override HeaderTemplate and not entire 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>

Upvotes: 1

Related Questions