Home > DeveloperSection > Forums > WPF Some styles not applied on DataTemplate controls
Brad Pitt
Brad Pitt

Total Post:61

Posted on    August-16-2013 5:51 AM


 1 Reply(s)
 1402  View(s)
Rate this:
Hi Developers!

I am trying to learn something about WPF and I am quite amazed by its flexibility. 

However, I have hit a problem 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 (I just use Kaxaml and XamlPadX to view the result) 





    <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 TargetType="{x:Type TextBlock}">

      <Setter Property="FontWeight" Value="Bold"/>


    <Style TargetType="{x:Type ProgressBar}">

      <Setter Property="Height" Value="10"/>

      <Setter Property="Width" Value="100"/>

      <Setter Property="Foreground" Value="Red"/>


    <XmlDataProvider x:Key="TestData" XPath="/TestData">


        <TestData xmlns="">


            <Name>Item 1</Name>




            <Name>Item 2</Name>






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



      <Border Height="45" Width="120" Margin="5,5">

        <StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">

          <TextBlock HorizontalAlignment="Center" Text="Item 2"/>

          <ProgressBar Value="50"/>




    <ListBox Margin="10,10"  Width="140" ItemsSource="{Binding Source={StaticResource TestData}, XPath=TestElement}"/>



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 for above anywhere and thus would like to ask if someone would be kind enough to explain this behaviour in lamens' terms with possible links to technical description, i.e. to MSDN or so. 

Thanks in advance for you support!

shreesh chandra shukla
shreesh chandra shukla

Total Post:105

Posted on    August-17-2013 7:07 AM


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


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

Don't want to miss updates? Please click the below button!

Follow MindStick