Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4127304
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:08:50+00:00 2026-05-21T00:08:50+00:00

I know a ControlTemplate is suppose to replace the default one entirely and this

  • 0

I know a ControlTemplate is suppose to replace the default one entirely and this seems to work when not using a HierarchicalDataTemplate. However using a HierarchicalDataTemplate my control template seems to be partially used – the TreeViewItem is the control I specified containing an image etc. BUT still appears as a node with the default expander to show its children – not specified in my Template (I want my children to always be shown, but thats beside the point). It looks like this:

      <TreeView>
        <TreeView.Resources>

            <Style x:Key="MyNodeStyle" TargetType="TreeViewItem">
                <Setter Property="SnapsToDevicePixels" Value="true" />
                <Setter Property="OverridesDefaultStyle" Value="true"/>
                <Setter Property="IsExpanded" Value="true"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TreeViewItem">
                            <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                                <Border CornerRadius="8" BorderThickness="1" Padding="2" Margin="4, 6" BorderBrush="{StaticResource itemBorderBrush}" Background="{StaticResource itemBackgroundBrush}" x:Name="border">
                                    <DockPanel LastChildFill="False">
                                        <StackPanel Orientation="Vertical" DockPanel.Dock="Top" Height="80">
                                            <TextBlock Text="{Binding Path=DisplayValue}" HorizontalAlignment="Center" FontWeight="Bold"/>
                                            <Image Source="MyNode.png" Stretch="None"/>
                                            <TextBlock Text="{Binding Path=Notes}" TextWrapping="Wrap" Width="150"/>                                                   
                                        </StackPanel>
                                    </DockPanel>
                                </Border>
                                <ItemsPresenter />
                            </StackPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" IsItemsHost="True" HorizontalAlignment="Center" />
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <HierarchicalDataTemplate DataType="{x:Type src:MyNode}" ItemsSource="{Binding Path=Children}" >
                <TreeViewItem Style="{StaticResource MyNodeStyle}"/>
            </HierarchicalDataTemplate>

        </TreeView.Resources>

        <!-- Arrange the root items horizontally. -->
        <TreeView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel IsItemsHost="True" Orientation="Horizontal" HorizontalAlignment="Center" />
            </ItemsPanelTemplate>
        </TreeView.ItemsPanel>

    </TreeView>

For some reason only when using a HierarchicalDataTemplate the ItemsPanel and Setter does not seem to be applied and the children are presented in the default expander. How did that expander get there when I am using my own ControlTemplate!?@#$

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T00:08:50+00:00Added an answer on May 21, 2026 at 12:08 am

    I don’t think you should put the TreeViewItem inside your HierarchicalDataTemplate.

    Try this:

    <HierarchicalDataTemplate DataType="{x:Type src:MyNode}" ItemsSource="{Binding Path=Children}" >
        <StackPanel Orientation="Vertical" DockPanel.Dock="Top" Height="80">
            <TextBlock Text="{Binding Path=DisplayValue}" HorizontalAlignment="Center" FontWeight="Bold"/>
            <Image Source="MyNode.png" Stretch="None"/>
            <TextBlock Text="{Binding Path=Notes}" TextWrapping="Wrap" Width="150"/>
        </StackPanel>
    </HierarchicalDataTemplate>
    

    Now, your template becomes:

    <ControlTemplate TargetType="TreeViewItem">
        <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
            <Border CornerRadius="8" BorderThickness="1" Padding="2" Margin="4, 6" x:Name="border">
                <DockPanel LastChildFill="False">
                    <ContentPresenter ContentSource="Header" />
                </DockPanel>
            </Border>
            <ItemsPresenter />
        </StackPanel>
    </ControlTemplate>
    

    Is that how you intended it to look?

    Edit: the original expanders are probably there because you only use your style for child items – make your style the ItemContainerStyle for the treeview:

    <Window.Resources>
        <Style x:Key="MyNodeStyle" TargetType="TreeViewItem">
            ....
    
    <TreeView ItemContainerStyle="{StaticResource MyNodeStyle}">
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to using the ControlTemplate . I am writing my first control
i have made a template that look like this : <ControlTemplate x:Key=onoffValue TargetType={x:Type Control}>
I know this question was asked before, but the answer didn't work for me.
Know of an OCAML/CAML IDE? Especially one that runs on Linux?
Know of any good libraries for this? I did some searches and didn't come
Know this might be rather basic, but I been trying to figure out how
Know if it's possible to access the iPhone compass in Safari using JavaScript? I
I know this might be a no-brainer, but please read on. I also know
I'm overriding the styles of my ListBoxItems with a ControlTemplate, however by doing that,
Why doesn't this work: <Style TargetType=s:Substance> <Setter Property=Template> <Setter.Value> <DataTemplate> <StackPanel> <TextBlock Text={TemplateBinding Name}/>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.