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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:09:31+00:00 2026-05-16T05:09:31+00:00

Well the problem here is that if you have one DataTemplate for a type

  • 0

Well the problem here is that if you have one DataTemplate for a type of object, each time the object is present, the DataTemplate will be created, that sounds about right.

My problem is that our DataTemplate is sort of heavy AND the app could have around 1000 objects using that DataTemplate.

I created a simplistic example where I created 5 Person objects and I bind them to a ListView. The DataTemplate for the Person object is a Grid with a Label and a ContextMenu with 2 MenuItems. For simplicity I just want to concentrate in the MenuItems. Using a memory profiler, I can see that there are a total of 10 MenuItem objects (2 per Person, 5 Person * 2 MenuItem = 10 MenuItem), and I want to know if there is a way to avoid this. Say, every Person object should share the same MenuItem reference, to avoid duplicating the DataTemplate each time a person is created.

These are the results in the memory profiler.

Thanks!

This is my code:

C#

using System.Collections.Generic;
using System.Windows;

namespace MenuItemsTest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            this.DataContext = new List<Person>()
            {
                new Person("Jim Morrison"),
                new Person("Jimmy Page"),
                new Person("Jimmy Hendrix"),
                new Person("Janis Joplin"),
                new Person("Peter Frampton")
            };
        }
    }

    public class Person
    {
        public string Name { get; set; }

        public Person(string name)
        {
            this.Name = name;
        }
    }
}

XAML:

<Window x:Class="MenuItemsTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MenuItemsTest"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:Person}">
            <Grid>
                <Grid.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Select" />
                        <MenuItem Header="Deselect" />
                    </ContextMenu>
                </Grid.ContextMenu>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Label Content="{Binding Name}" />
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ListView ItemsSource="{Binding}" />
    </Grid>
</Window>
  • 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-16T05:09:32+00:00Added an answer on May 16, 2026 at 5:09 am

    DataTemplates are the way to go but you don’t want all of them instansiated if only say 20 are visible on screen:

    Try virtualizing stackpanel. If you don’t need the list view functionality opt for ListBox. Simpler control => better performance.

    It’s very important that you don’t let ListView dimension get unbounded (ie don’t set a scrollviewer around it) as that will instansiate all data templates even if you use virtualizing stackpanel.

      <ListView 
         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
         ScrollViewer.VerticalScrollBarVisibility="Auto"
         VirtualizingStackPanel.IsVirtualizing="True"
         VirtualizingStackPanel.VirtualizationMode="Standard" 
         ItemsSource="{Binding}"
         />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well here's my problem I have three tables; regions, countries, states. Countries can be
Okay... So, I came across the following problem: I have one table that works
Well, this is my first post here and really enjoying the site. I have
We have an app that uses simple one way binding with a GridView to
I have a listbox defined like this: <ListBox.ItemTemplate> <DataTemplate> <ItemsControl> <!-- Contents here -->
Imagine that I have a Debtor class. With Hibernate, I will define the class
I have php scripts that I have to run on linux as well as
It is a well known problem to every web developer. As far as I
Well, after a long time writing .net programs in C# I started to feel
Well this is incredibly frustrating. After being nagged by Rails that I need to

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.