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

  • SEARCH
  • Home
  • 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 44121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:36:09+00:00 2026-05-10T15:36:09+00:00

At run time I want to dynamically build grid columns (or another display layout)

  • 0

At run time I want to dynamically build grid columns (or another display layout) in a WPF ListView. I do not know the number and names of the columns before hand.

I want to be able to do:
MyListView.ItemSource = MyDataset;
MyListView.CreateColumns();

  • 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. 2026-05-10T15:36:10+00:00Added an answer on May 10, 2026 at 3:36 pm

    i’d try following approach:

    A) you need to have the list box display grid view – i believe this you’ve done already
    B) define a style for GridViewColumnHeader:

            <Style TargetType='{x:Type GridViewColumnHeader}' x:Key='gridViewColumnStyle'>             <EventSetter Event='Click' Handler='OnHeaderClicked'/>             <EventSetter Event='Loaded' Handler='OnHeaderLoaded'/>         </Style> 

    in my case, i had a whole bunch of other properties set, but in the basic scenario – you’d need Loaded event. Clicked – this is useful if you want to add sorting and filtering functionality.

    C) in your listview code, bind the template with your gridview:

        public MyListView()     {         InitializeComponent();         GridView gridViewHeader = this.listView.View as GridView;         System.Diagnostics.Debug.Assert(gridViewHeader != null, 'Expected ListView.View should be GridView');         if (null != gridViewHeader)         {             gridViewHeader.ColumnHeaderContainerStyle = (Style)this.FindResource('gridViewColumnStyle');         }     } 

    D) then in you OnHeaderLoaded handler, you can set a proper template based on the column’s data

        void OnHeaderLoaded(object sender, RoutedEventArgs e)     {         GridViewColumnHeader header = (GridViewColumnHeader)sender;         GridViewColumn column = header.Column; 

    //select and apply your data template here.

            e.Handled = true;     } 

    E) I guess you’d need also to acquire ownership of ItemsSource dependency property and handle it’s changed event.

                ListView.ItemsSourceProperty.AddOwner(typeof(MyListView), new PropertyMetadata(OnItemsSourceChanged));          static void OnItemsSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)         {             MyListView view = (MyListView)sender;             //do reflection to get column names and types             //and for each column, add it to your grid view:             GridViewColumn column = new GridViewColumn();             //set column properties here...             view.Columns.Add(column);         } 

    the GridViewColumn class itself doesn’t have much properties, so you might want to add some information there using attached properties – i.e. like unique column tag – header most likely will be used for localization, and you will not relay on this one.

    In general, this approach, even though quite complicated, will allow you to easily extend your list view functionality.

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

Sidebar

Ask A Question

Stats

  • Questions 51k
  • Answers 51k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer In the script you can do [Diagnostics.Process]::Start('notepad.exe') May 11, 2026 at 6:27 am
  • added an answer Ferruccio's answer contains most of the insight. However, Pukku made… May 11, 2026 at 6:27 am
  • added an answer Ok lets see if I understand the question first. If… May 11, 2026 at 6:27 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.