Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Adding multiple columns in WPF TreeView at runtime is very slow

$
0
0

Hi Masters,

I have a made a customised Hierarchical TreeView which has a capability of creating multiple columns like a grid.

I have requirement to add/remove multiple columns in the TreeView at runtime (let's say on any button click).

I did this in code behind of xaml like below. But it is very slow in adding columns to the tree view specially when you have a very deep tree view.

treeView.Columns.Add takes so much time. Is there any other way to do it  or Fix this code ?

public List<Zone> SelectedZones
{
	get { return (List<Zone>)GetValue(SelectedZonesProperty); }
	set { SetValue(SelectedZonesProperty, value); }
}
public static readonly DependencyProperty SelectedZonesProperty =
	DependencyProperty.Register("SelectedZones", typeof(List<Zone>), typeof(PIRATTreeView), new UIPropertyMetadata(new List<Zone>()));
private void OnRebuildTree(object sender, DependencyPropertyChangedEventArgs e)
{
	if (SelectedZones == null || SelectedZones.Count == 0) return;
	if ((bool)e.NewValue)
	{
		while(treeView.Columns.Count > 1)
			treeView.Columns.RemoveAt(1);//dont delete the first element
		var zone = SelectedZones.FirstOrDefault();
		if (zone.AmountTypes == null || zone.AmountTypes.Count == 0) return;
		int count = 0;
		foreach (var field in zone.AmountTypes)
		{
			treeView.Columns.Add(new GridViewColumn() { Width = 100, Header = field.Value.HeaderString
				, DisplayMemberBinding = new Binding(string.Format("AmountTypes[{0}].AmountValue", count))});
			count++;
		}
	}
}

Regards


Ashish Dhyani


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>