locked
Header template is setted but the header is null RRS feed

  • Question

  • I set a header template using dynamic XAML:
    DataGridTemplateColumn tc = new DataGridTemplateColumn();
    
    public static string GetHeaderDataTemplate(string bindingPath)
    {
    	return @" 
    		<DataTemplate
    		xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    		xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" >
    			<TextBlock Text=""{Binding Path=" + bindingPath + @"}""
    				TextWrapping=""Wrap"" 
     				Foreground=""#FF1EA800"" 
    				FontWeight=""Bold"" /> 
    		</DataTemplate>";
    } 
    
    tc.HeaderTemplate = (DataTemplate)XamlReader.Parse(GetHeaderDataTemplate(someText));
    
    


    But there is nothing in the place of datagrid column header. It is just an empty place. So I tried to get access to the header and got null reference exception.
    How to set a header data template in the right way?





    Tuesday, July 19, 2011 12:36 PM

Answers

  • Hi user808128,

    I think there is no need to using the binding extension. You can just pass the name of the property as a paremeter and use it to set Text propety directly.

    Like,

    "<TextBlock Text= """ + proertyName

     

    If you still have any questions or concerns about this issue, please feel free to let me know.

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Min Zhu Thursday, August 4, 2011 2:15 AM
    Wednesday, July 27, 2011 10:14 AM

All replies

  • Hello,

    Try creating the DataTemplate in code:

        public DataTemplate GetHeaderDataTemplate(string bindingPath)
        {
          DataTemplate headerTemplate = new DataTemplate();
          FrameworkElementFactory textBlockFactory = new FrameworkElementFactory(typeof(TextBlock));
    
          Binding textBinding = new Binding(bindingPath);
          textBlockFactory.SetBinding(TextBlock.TextProperty,textBinding);
    
          textBlockFactory.SetValue(TextBlock.TextWrappingProperty,TextWrapping.Wrap);
          textBlockFactory.SetValue(TextBlock.ForegroundProperty,new BrushConverter().ConvertFrom("#FF1EA800"));
          textBlockFactory.SetValue(TextBlock.FontWeightProperty,FontWeights.Bold);
    
          headerTemplate.VisualTree = textBlockFactory;
    
          return headerTemplate;
        }
    
    ...
    
      DataGridTemplateColumn tc = new DataGridTemplateColumn();
      tc.HeaderTemplate = GetHeaderDataTemplate("SomeName");
    


    Hope it helps.

     

     

    Tuesday, July 19, 2011 1:31 PM
  • Hi, user808128!

    Data template does not provide any content. It just makes a view of some content. And if we want to see something - we have to provide both template and content.

    In case of data grid column header we need to provide both "HeaderTemplate" and "Header".

    Your code works just fine. It makes data grid column header template very well. Let`s assign some Header for that template. Here is an example:

     

    <Window x:Class="WpfApplication13.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Column Header Template Example">
     <DataGrid x:Name="MyGrid" />
    </Window>
    
    


     

     

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Markup;
    
    namespace WpfApplication13
    {
     public partial class MainWindow : Window
     {
      public MainWindow()
      {
       InitializeComponent();
    
       DataGridTemplateColumn tc = new DataGridTemplateColumn();
       tc.HeaderTemplate = (DataTemplate)XamlReader.Parse(GetHeaderDataTemplate("FullName"));
    
       var person = new { FullName = "John Smith" };
       tc.Header = person;
    
       MyGrid.Columns.Add(tc);
      }
    
      public static string GetHeaderDataTemplate(string bindingPath)
      {
       return @" 
    		  <DataTemplate
    		   xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    		   xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" >
    			  <TextBlock Text=""{Binding Path=" + bindingPath + @"}""
    				  TextWrapping=""Wrap"" 
     				  Foreground=""#FF1EA800"" 
    				  FontWeight=""Bold"" /> 
    		  </DataTemplate>";
      }
     }
    }
    
    

    Hope, this helps.

     

     


    Tuesday, July 19, 2011 2:31 PM
  • Thanks, Alexander.

    I want to specify that because the parameter of the GetHeaderDataTemplate method is used not as object with fields but as primitive data type "string" it's perhaps better to use not BindingPath property but the BindsDirectlyToSource setted with "true" in tne TextBlock of DataTemplate:

     

     

     

    public static string GetHeaderDataTemplate()
    {
    return @" 
    <DataTemplate
    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" >
    <TextBlock Text=""{Binding BindsDirectlyToSource=True}""
    TextWrapping=""Wrap"" 
    Foreground=""#FF1EA800""
    FontWeight=""Bold"" /> 
    </DataTemplate>";
    }
    



     

    As you can see in this case I needn't the parameter in the GetHeaderDataTemplate method.


    Thursday, July 21, 2011 7:06 AM
  • Hi user808128,

    I think there is no need to using the binding extension. You can just pass the name of the property as a paremeter and use it to set Text propety directly.

    Like,

    "<TextBlock Text= """ + proertyName

     

    If you still have any questions or concerns about this issue, please feel free to let me know.

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Min Zhu Thursday, August 4, 2011 2:15 AM
    Wednesday, July 27, 2011 10:14 AM
  • Hi user808128,

    We have not heard from you in a couple of days.

    Please post back at your convenience if we can assist further.

    Enjoy your day!


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 1, 2011 2:20 AM
  • Hi user808128,

    I am marking this issue as "Answered". If you have any new questions or concerns about this issue, please feel free to let me know.

    Thank you and have a nice day!


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, August 4, 2011 2:15 AM
  • This answer helps me a lot. Fro two days i'm searching about this how to get elements from the header templates and I was trying everything without this. Sometimes the simple solutions are better than the difficult ones. Thanks.
    Wednesday, October 1, 2014 11:37 AM
  • I tried this, but he saids :"Impossible de créer l'instance de '{clr-namespace:EspBtpWpf.UserControls}ucDataGridColHead'.

    He resolve the namespace "Local" but he can't instanciate ucDataGridColHead (it's an unserControl).

    Howether, the same XML in XAML working well.

            public static string GetHeaderDataTemplate(string pTitre) {
                return @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:local=""clr-namespace:EspBtpWpf.UserControls"">
                    <Grid>
                        <Grid.RowDefinitions><RowDefinition /><RowDefinition /><RowDefinition /></Grid.RowDefinitions>
                        <local:ucDataGridColHead Grid.Row=""0"" x:Name=""lblRegLib"" Tag=""etat"" Title=""" + pTitre + @""" CharIcon="""" />
                        <Rectangle Grid.Row=""1"" Fill=""{StaticResource scbGrey2}"" Margin=""-7, 2"" Height=""1.5"" />
                        <TextBox x:Name=""txtRegLib"" Grid.Row=""2"" Margin=""2"" Height=""24"" Visibility=""{Binding ElementName=ucTabBord, Path=ModeAff, Converter={StaticResource BoolToVisibility}, ConverterParameter=true}"" />
                    </Grid></DataTemplate>";
            }


    • Edited by DavidOutlook Wednesday, December 7, 2016 9:19 AM
    Wednesday, December 7, 2016 9:19 AM