글쎄, 여기서 문제는 개체 유형에 대해 하나의 DataTemplate이 있고 개체가있을 때마다 DataTemplate이 생성된다는 것입니다.WPF DataTemplate 성능
내 문제는 우리의 DataTemplate이 무겁고 그 응용 프로그램이 DataTemplate을 사용하는 약 1000 개의 개체를 가질 수 있다는 것입니다.
저는 5 Person 개체를 생성하고 ListView에 바인딩하는 간단한 예제를 만들었습니다. Person 개체의 DataTemplate은 Label이있는 Grid 및 2 개의 MenuItem이있는 ContextMenu입니다. 간단히하기 위해 MenuItems에 집중하려고합니다. 메모리 프로파일 러를 사용하면 총 10 개의 MenuItem 객체 (Person 당 2 개, 5 Person * 2 MenuItem = 10 MenuItem)가 있음을 알 수 있습니다.이를 피할 수있는 방법이 있는지 알고 싶습니다. 예를 들어, Person 객체가 생성 될 때마다 DataTemplate을 복제하지 않으려면 모든 Person 객체가 동일한 MenuItem 참조를 공유해야합니다.
다음은 메모리 프로파일 러의 결과입니다.
감사합니다!
이
내 코드입니다 :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>
가상화 스택 패널을 사용하여 나는 꽤 복잡한 데이터 템플릿으로 수십억 개의 행을 지니고있었습니다 (저는 MemoryMappedFile로 놀고있었습니다). – FuleSnabel