TreeListView Columns binding (or creating dynamically)

Grids for WPF Forum

Posted 2 years ago by Ryan Schwoerer - Sr Software Engineer, Veeco Instruments Inc.
Version: 17.1.0650
Avatar

I am trying to get started with the new TreeListView, and I have somewhat of an odd model.

I need to dynamically create some of the columns, and have some be fixed. That is, I will always have the first three columns, and the remaining n columns will be dynamic.

The Columns collection on the TreeListView is readonly, so I can't bind a collection on the viewmodel to it (where the columns would be created). 

Do you have a suggestion on a way to create the columns dynamically, maybe starting with all of them from i.e. a collection and I can work back into the 'fixed' vs. 'dynamic' problem.

Comments (3)

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Ryan,

That's true, you can't bind to create columns themselves.  The first question I'd ask is, do you have a fixed set of total columns (like 10) or do they really vary, where you might have any number of random columns eventually?  If the former, there is a simple IsVisible property on each column that you can set to false on ones that should be hidden.

Otherwise, I'd probably recommend just handling some event notification for when you need to update the columns collection.  When you get that, remove all columns after the fixed 3 and dynamically create the new columns in code, adding them to the Columns collection.  


Actipro Software Support

Posted 2 years ago by Ryan Schwoerer - Sr Software Engineer, Veeco Instruments Inc.
Avatar

Thanks for the quick reply.

The number of columns could be anything, after the first 2.

 

I'm able to generate the columns using an attached property (below for reference, slightly modified), however I'm not quite sure how to go about hooking up the information that should be in that column.

i.e. in the example I have a 'Columns' collection on the ViewModel, the Root of the TreeListView is 'StepsForTreeView', each step has another collection in it which becomes the data for the columns. Using 'DisplayMemberBinding' on the dynamically created columns works, but their template is just a TextBlock, I cannot figure out how to appropriately assign a CellTemplate.

 

(I know this is a little off the beaten path, and not specifically an 'issue' with the control, just hoping for a suggestion from the developer. THANKS)

 

<actiproGrids:TreeListView recipeStepper:CollectionToTreeListViewColumnHelper.ControlPoints="{Binding Columns}" RootItem="{Binding StepsForTreeView}">

 

public class CollectionToTreeListViewColumnHelper : DependencyObject
{
	public static ColumnList GetColumns(DependencyObject d)
	{
		return (ColumnList)d.GetValue(ColumnsProperty);
	}

	public static void SetColumns(DependencyObject d, ColumnList value)
	{
		d.SetValue(ColumnsProperty, value);
	}

	public static readonly DependencyProperty
		ColumnsProperty = DependencyProperty.RegisterAttached("Columns",
			typeof(ColumnList),
			typeof(CollectionToTreeListViewColumnHelper),
			new PropertyMetadata(
				new PropertyChangedCallback(OnColumnsChanged)));

	private static void OnColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
	{
		var tree = (TreeListView)d;
		var columnss = (ColumnList)e.NewValue;

		if (columns == null) return;
 
                int pt = 0;
		foreach (var col in columns)
		{
			var newcolumn = new TreeListViewColumn();
			newcolumn.Header = col.Name;
			//newcolumn.CellTemplate = // tbd.. 
                        //newcolumn.DisplayMemberBinding = new Binding($"Actions[{pt}]");  // this does not work either. needs Template.
			tree.Columns.Add(newcolumn);
                        
                        pt++;  
		}
	}
}
Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Ryan,

What you are doing there generally seems fine.  As you saw, you want to probably be able to set the CellTemplate (where you have it commented out).  What you can do is have all your DataTemplates in your Application.Resources, each with a unique x:Key.  Then call tree.TryFindResource() to locate the appropriate DataTemplate based on a matching resource key that is tracked in your colum view model.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 21 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.