locked
How apply Style to all TextBlock within DataTemplate? RRS feed

  • Question

  • I have a ListBox with mulitple DataTemplates.  I want to trigger the styling of all the TextBlocks within all of the DataTemplates in that ListBox.  But the styling only works when the TextBlock is the added directly to the Items property of the ListBox. 

    How to style all the TextBlocks of all the dataTemplates of the ListBox?

    thanks,

    <Window x:Class="wpfDemo.tpDemo.steve2t"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:wpfDemo.tpDemo"
            mc:Ignorable="d"
            Title="steve2t" Height="300" Width="500">
        <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
      </Grid.RowDefinitions>
    
      <ListBox Grid.Row="0" x:Name="ListBox1">
        <ListBox.Resources>
    
          <Style TargetType = "TextBlock">
            <Setter Property = "Foreground" Value = "Red" />
            <Style.Triggers>
              <Trigger Property = "IsMouseOver" Value = "True">
                <Setter Property = "Foreground" Value = "Green" />
                <Setter Property="FontWeight" Value="ExtraBold" />
                <Setter Property="FontSize" Value="24"/>
              </Trigger>
            </Style.Triggers>
          </Style>
    
          <DataTemplate DataType="{x:Type local:DemoDetail}">
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="80"/>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="70"/>
                <ColumnDefinition Width="150"/>
              </Grid.ColumnDefinitions>
    
              <TextBlock Text="{Binding OrdNum}" FontSize="12" Grid.Column="0" />
              <TextBlock Text="{Binding ProdCode}" Foreground="Red" Grid.Column="1" />
              <TextBlock Text="{Binding Quantity}" Grid.Column="2" />
              <TextBlock Grid.Column="3">DemoDetail</TextBlock>
            </Grid>
          </DataTemplate>
    
          <DataTemplate DataType="{x:Type local:DemoHeader}">
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="80"/>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="150"/>
              </Grid.ColumnDefinitions>
    
              <TextBlock Text="{Binding CustNum}" FontSize="12" Grid.Column="0" />
              <TextBlock Text="{Binding OrdNum}" Foreground="Pink" Grid.Column="1" />
              <TextBlock Grid.Column="2">DemoHeader</TextBlock>
            </Grid>
          </DataTemplate>
    
        </ListBox.Resources>
      </ListBox>
      </Grid>
    </Window>
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    
    namespace wpfDemo.tpDemo
    {
      /// <summary>
      /// Interaction logic for steve2t.xaml
      /// </summary>
      public partial class steve2t : Window
      {
        public steve2t()
        {
          InitializeComponent();
          this.Loaded += Steve2t_Loaded;
        }
    
        private void Steve2t_Loaded(object sender, RoutedEventArgs e)
        {
          FillListBox();
        }
    
        void FillListBox()
        {
          {
            var oh = new DemoHeader("A1234", "BS332");
            ListBox1.Items.Add(oh);
          }
          {
            var od = new DemoDetail("A1234", "LA1PPK", 2);
            ListBox1.Items.Add(od);
          }
    
          // adding TextBlock directly to the ListBox. This TextBlock will be styled.
          // But the TextBlocks within the DataTemplates are not being styled. Why??
          {
            var tb = new TextBlock()
            {
              Text = "Hazard class: UN1266"
            };
            ListBox1.Items.Add(tb);
          }
        }
      }
    
      public class DemoDetail
      {
        public string OrdNum { get; set; }
        public string ProdCode { get; set; }
        public int Quantity { get; set; }
    
        public DemoDetail(string ordnum, string prodcode, int quantity)
        {
          this.OrdNum = ordnum;
          this.ProdCode = prodcode;
          this.Quantity = quantity;
        }
      }
    
      public class DemoHeader
      {
        public string OrdNum
        { get; set; }
    
        public string CustNum
        { get; set; }
    
        public DemoHeader(string OrdNum, string CustNum)
        {
          this.OrdNum = OrdNum;
          this.CustNum = CustNum;
        }
      }
    }
    

    Thursday, September 29, 2016 3:45 PM

Answers

  • Please refer to my answer in the following thread: https://social.msdn.microsoft.com/Forums/vstudio/en-US/e014042d-9bfe-4f7d-ae43-769862bb214f/textblock-ignores-style-why?forum=wpf. The implicit style for the TextBlock won't get applied automatically but you could give your base style an x:Key and add empty styles that are based on this one in the Resources section of each DataTemplate:

    <ListBox.Resources>
                    <Style x:Key="tbStyle" TargetType = "TextBlock">
                        <Setter Property = "Foreground" Value = "Blue" />
                        <Style.Triggers>
                            <Trigger Property = "IsMouseOver" Value = "True">
                                <Setter Property = "Foreground" Value = "Green" />
                                <Setter Property="FontWeight" Value="ExtraBold" />
                                <Setter Property="FontSize" Value="24"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
    
                    <DataTemplate DataType="{x:Type local:DemoDetail}">
                        <DataTemplate.Resources>
                            <Style TargetType="TextBlock" BasedOn="{StaticResource tbStyle}" />
                        </DataTemplate.Resources>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="80"/>
                                <ColumnDefinition Width="100"/>
                                <ColumnDefinition Width="70"/>
                                <ColumnDefinition Width="150"/>
                            </Grid.ColumnDefinitions>
    
                            <TextBlock Text="{Binding OrdNum}" FontSize="12" Grid.Column="0" />
                            <TextBlock Text="{Binding ProdCode}" Grid.Column="1" />
                            <TextBlock Text="{Binding Quantity}" Grid.Column="2" />
                            <TextBlock Grid.Column="3">DemoDetail</TextBlock>
                        </Grid>
                    </DataTemplate>
    
                    <DataTemplate DataType="{x:Type local:DemoHeader}">
                        <DataTemplate.Resources>
                            <Style TargetType="TextBlock" BasedOn="{StaticResource tbStyle}" />
                        </DataTemplate.Resources>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="80"/>
                                <ColumnDefinition Width="100"/>
                                <ColumnDefinition Width="150"/>
                            </Grid.ColumnDefinitions>
    
                            <TextBlock Text="{Binding CustNum}" FontSize="12" Grid.Column="0" />
                            <TextBlock Text="{Binding OrdNum}" Grid.Column="1" />
                            <TextBlock Grid.Column="2">DemoHeader</TextBlock>
                        </Grid>
                    </DataTemplate>
                </ListBox.Resources>
    


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Steve Richter Thursday, September 29, 2016 9:13 PM
    Thursday, September 29, 2016 8:11 PM