---
title: "WPF Some styles not applied on DataTemplate controls"  
description: "WPF Some styles not applied on DataTemplate controls"  
author: "Anonymous User"  
published: 2013-08-16  
updated: 2013-08-17  
canonical: https://www.mindstick.com/forum/1377/wpf-some-styles-not-applied-on-datatemplate-controls  
category: "wpf"  
tags: ["wpf"]  
reading_time: 2 minutes  

---

# WPF Some styles not applied on DataTemplate controls

Hi [Developers](https://www.mindstick.com/articles/12875/blunders-you-must-avoid-while-hiring-java-developers-to-get-the-best-fulfilling-your-needs)!

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to learn something about WPF and I am quite amazed by its flexibility.

However, I have hit a [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) with Styles and DataTemplates, which is little bit confusing. I have defined below test page to play around a bit with styles etc and found that the Styles defined in <Page.Resources> for Border and TextBlock are not applied in the DataTemplate, but Style for ProgressBar defined in exactly the same way is applied.

[Source code](https://www.mindstick.com/forum/33476/pls-send-me-source-code-for-changing-passward-in-asp-dot-net-i-tried-so-much-from-google-but-its-not-workining) (I just use Kaxaml and XamlPadX to view the result)

```
<Page  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">   <Page.Resources>     <Style TargetType="{x:Type Border}">      <Setter Property="Background" Value="SkyBlue"/>      <Setter Property="BorderBrush" Value="Black"/>      <Setter Property="BorderThickness" Value="2"/>      <Setter Property="CornerRadius" Value="5"/>    </Style>     <Style TargetType="{x:Type TextBlock}">      <Setter Property="FontWeight" Value="Bold"/>    </Style>     <Style TargetType="{x:Type ProgressBar}">      <Setter Property="Height" Value="10"/>      <Setter Property="Width" Value="100"/>      <Setter Property="Foreground" Value="Red"/>    </Style>     <XmlDataProvider x:Key="TestData" XPath="/TestData">      <x:XData>        <TestData xmlns="">          <TestElement>            <Name>Item 1</Name>            <Value>25</Value>          </TestElement>          <TestElement>            <Name>Item 2</Name>            <Value>50</Value>          </TestElement>        </TestData>      </x:XData>    </XmlDataProvider>     <HierarchicalDataTemplate DataType="TestElement">      <Border Height="45" Width="120" Margin="5,5">        <StackPanel Orientation="Vertical" Margin="5,5" VerticalAlignment="Center" HorizontalAlignment="Center">          <TextBlock HorizontalAlignment="Center" Text="{Binding XPath=Name}"/>          <ProgressBar Value="{Binding XPath=Value}"/>        </StackPanel>      </Border>    </HierarchicalDataTemplate>   </Page.Resources>   <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">    <StackPanel Orientation="Vertical" VerticalAlignment="Center">      <Border Height="45" Width="120" Margin="5,5">        <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">          <TextBlock HorizontalAlignment="Center" Text="Item 1"/>          <ProgressBar Value="25"/>        </StackPanel>      </Border>      <Border Height="45" Width="120" Margin="5,5">        <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">          <TextBlock HorizontalAlignment="Center" Text="Item 2"/>          <ProgressBar Value="50"/>        </StackPanel>      </Border>    </StackPanel>    <ListBox Margin="10,10"  Width="140" ItemsSource="{Binding Source={StaticResource TestData}, XPath=TestElement}"/>  </StackPanel></Page> 
```

I suspect it has something to do with default styles etc, but more puzzling is why some Styles are applied and some not. I cannot find an easy [explanation](https://yourviews.mindstick.com/view/84608/what-are-hindenburg-s-allegations-against-adani-detailed-explanation) for above anywhere and thus would like to ask [if someone](https://answers.mindstick.com/qa/99016/is-there-any-device-that-can-detect-if-someone-opens-your-backpack) would be kind enough to [explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) this behaviour in lamens' terms with possible links to [technical](https://www.mindstick.com/articles/12830/a-look-at-the-technical-features-and-specifications-of-type-c-hub) description, i.e. to MSDN or so.

Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams) for you [support](https://yourviews.mindstick.com/view/286/modi-support-for-sportsperson)!

\

## Replies

### Reply by shreesh chandra shukla

Hi!

I've looked into this also, and I personally think it's a bug. I've noticed that the style is set if you name your styles like so:

```
<Style x:Key="BorderStyle"
TargetType="{x:Type Border}">etc... and explicitly set your DataTemplate to use those styles: <HierarchicalDataTemplate DataTemplate="TestElement">  <Border Height="45" Width="120" Margin="5,5",
Style="{StaticResource BorderStyle}"> 
```

I think that it's possible that for DataTemplates (and maybe ControlTemplates), they default to having a null style, unless you explicitly set them.

That to me is not meant to happen - it's not a logical way of WPF working...


---

Original Source: https://www.mindstick.com/forum/1377/wpf-some-styles-not-applied-on-datatemplate-controls

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
