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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:30:54+00:00 2026-05-19T10:30:54+00:00

I have a WPF ListView, which has a Grid as an ItemsPanelTemplate. I display

  • 0

I have a WPF ListView, which has a Grid as an ItemsPanelTemplate. I display my items in the correct column and row based on a property of the item. But I would like to put some controls in the empty cells of my grid.

This is a simplified version of my code and xaml:

In the resources:

<ItemsPanelTemplate x:Key="TheTemplate">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
    </Grid>
</ItemsPanelTemplate>

In the xaml:

<Controls:CustomListView ItemsSource="{Binding TheCollection}"
                            ItemsPanel="{DynamicResource TheTemplate}">
</Controls:CustomListView>

Finally, in my CustomListView:

protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
    base.PrepareContainerForItemOverride(element, item);
    var viewModel = item as DomainObject;
    if (viewModel != null)
    {
        element.SetValue(Grid.ColumnProperty, 1); //here I work with a converter, but this just simplifies it for StackOverflow
        element.SetValue(Grid.RowProperty, 1);
    }
}

NOTE: I know I’m casting to a DomainObject, but just bear with me, please.

What this will give me, is a grid with items in the correct row and column. But what if I want to display something in the empty cells, for example some text like ‘null’?

I can’t just add it to my template, because that crashes the application, saying the ItemsControl will create the necessary controls. I’ve tried accessing the grid/template in code-behind, but can’t quite find how to. Maybe I shouldn’t be using the ListView? Maybe there are other/better solutions?

  • 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-19T10:30:55+00:00Added an answer on May 19, 2026 at 10:30 am

    What I ended up doing was:

    • Make a Grid with all my ColumnDefinitions and RowDefinitions (I know how many I need)
    • In the code-behind of my View, I cast my DataContext to what I know it will be
    • I iterate over the collection, and for each item, I create a new Control and set its DataContext to the item
    • I then set the RowProperty and ColumnProperty and add this control to the Grid
    • I also remember where I added all these controls, so I can add empty controls to the Grid where there aren’t any yet.

    This is what I think I will change for performance reasons:
    – Instead of iteration over the collection of items, iterate over all the possible cells of the Grid, add a Control (always), and set the DataContext if there is a corresponding item.

    The reason to do this is performance. It takes about 2 secondes to fill the grid, and I’d like it to be faster.

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

Sidebar

Related Questions

I have following WPF ListView: <ListView Grid.Column=2 Grid.Row=1 Margin=0,53,12,6 Name=lvwProperties ItemsSource={Binding Path=SelectedPropertyItems, Mode=TwoWay} Grid.ColumnSpan=2>
I have a wpf control named DataPicker which has a dependency property named SelectedDate.
I have a WPF ListView which repeats the data vertically. I cannot figure out
I have a WPF Window which has a among other controls hosts a Frame.
I have a normal WPF window, let's call it TopLevel which has a bunch
So I have binded a List to a ListView where the List has items
I have a WPF ListView control for which I am dynamically creating columns. One
I have a WPF ListView that is bound to a BindingList<T>. The binding works
I have a ListView in WPF that is databound to a basic table that
I have WPF ListBox which is bound to a ObservableCollection, when the collection changes,

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.