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 848571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:57:46+00:00 2026-05-15T06:57:46+00:00

I am writing a WPF program in C# in which I have a ListView

  • 0

I am writing a WPF program in C# in which I have a ListView for which the columns will be populated at runtime. I would like to use a custom DataTemplate for the GridViewColumn objects in the ListView.

In the examples I have seen where the number of columns is fixed in advance, a custom DataTemplate is often created using something like the XAML below.

<DataTemplate x:Key="someKey">
    <TextBlock Text="{Binding Path=FirstName}" />
</DataTemplate>

This DataTemplate could also later be assigned to GridViewColumn.CellTemplate in the code-behind by calling FindResource(“someKey”). However, this alone is of no use to me, because in this example the Path element is fixed to FirstName. Really I need something where I can set the Path in code.

It is my impression that something along these lines may be possible if XamlReader is used, but I’m not sure how in practice I would do this. Any solutions are greatly appreciated.

  • 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-15T06:57:47+00:00Added an answer on May 15, 2026 at 6:57 am

    It is easy to build what you need using two DataTemplates working in concert: The outer DataTemplate simply sets the DataContext for the inner DataTemplate, as follows:

    <DataTemplate x:Key="DisplayTemplate">
      <Border ...>
        <TextBlock Text="{Binding}" ... />
      </Border>
    </DataTemplate>
    
    <DataTemplate x:Key="CellTemplate">
      <ContentPresenter Content="{Binding FirstName}"
                        ContentTemplate="{StaticResource DisplayTemplate}" />
    </DataTemplate>
    

    The only tricky thing is making it convenient to set this on a GridViewColumn. I would accomplish this with attached properties, allowing you to write:

    <GridViewColumn
      my:GVCHelper.DisplayPath="FirstName"
      my:GVCHelper.Template="{StaticResource DisplayTemplate}" />
    

    Or equivalently in code:

    var col = new GridViewColumn();
    GVCHelper.SetDisplayPath(col, "FirstName");
    GVCHelper.SetTemplate(col, (DataTemplate)FindResource("DisplayTemplate"));
    

    Either of these would cause the DataTemplate named “DisplayTemplate” to be used to display the FirstName in the column.

    The helper class would be implemented as:

    public class GVCHelper : DependencyObject
    {
      public static string GetDisplayPath(DependencyObject obj) { return (string)obj.GetValue(DisplayPathProperty); }
      public static void SetDisplayPath(DependencyObject obj, string value) { obj.SetValue(DisplayPathProperty, value); }
      public static readonly DependencyProperty DisplayPathProperty = DependencyProperty.RegisterAttached("DisplayPath", typeof(string), typeof(GVCHelper), new PropertyMetadata
      {
        PropertyChangedCallback = (obj, e) => Update(obj)
      });
    
      public static DataTemplate GetTemplate(DependencyObject obj) { return (DataTemplate)obj.GetValue(TemplateProperty); }
      public static void SetTemplate(DependencyObject obj, DataTemplate value) { obj.SetValue(TemplateProperty, value); }
      public static readonly DependencyProperty TemplateProperty = DependencyProperty.RegisterAttached("Template", typeof(DataTemplate), typeof(GVCHelper), new PropertyMetadata
      {
        PropertyChangedCallback = (obj, e) => Update(obj)
      });
    
      private static void Update(DependencyObject obj)
      {
        var path = GetDisplayPath(obj);
        var template = GetTemplate(obj);
        if(path!=null && template!=null)
        {
          var factory = new FrameworkElementFactory(typeof(ContentPresenter));
          factory.SetBinding(ContentPresenter.ContentProperty, new Binding(path));
          factory.SetValue(ContentPresenter.ContentTemplateProperty, template);
          obj.SetValue(GridViewColumn.CellTemplateProperty,
            new DataTemplate { VisualTree = factory };
        }
      }
    }
    

    How it works: Whenever the properties are both set, a new DataTemplate is constructed and the GridViewColumn.CellTemplate property is updated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.