For each player we want to list their positions and for each coach we want to have their title and to output their proper name in the UI.
Let’s say that we want to look at the composition of the coaches and the players. Let’s say that we wanted to go into even more depth. That’s because, probably obviously when put that way, because there is no hierarchy.Īvalonia TreeView with two layers of hierarchy More Complex Hierarchy You notice that for teams that don’t have players listed there is no arrow indicating a lower hiearchy. The new XAML creates the following view, with the additional hierarchy for teams that have players. “This is what Avalonia binds to for potential children objects of this object”.“This is what Avalonia supposed to present for this object”.Each data template only cares about its own specific hierarchy which is: The hierarchy is a function of the way the objects are structured in their parent/child relationship. Notice that we aren’t creating hierarchical templates per-se. Let’s make a simple Person class that stores a name: We just need to continue the hierarchy as we have previously in the Model and then tweak the corresponding data templates. Let’s say that we want to be able to explore the players on the team. The above therefore creates the view we originally were wanting:Īvalonia TreeView Initial Working View Adding More HierarchyĪlright, let’s say that we really want to look at the league information one layer down. Inside the block we define what we render for the current level of the hierarchy before Avalonia iterates on the children items. It has the additional ItemsSource property that we need to tell it about our hierarchy.
This is is similar to the HierarchicalDataTemplate class in WPF. Therefore in its definition (lines 19-21) we use a TreeDataTemplate not DataTemplate. In this case this is the Teams property on a conference. In the case of the Conference however we need to tell it about the children object. We are going to write a TextBlock whenever the control is trying to render a Team object. In the case of the Team class (lines 22-24) it is just like our previous examples you may have seen. We use the DataType keyword to give Avalonia a hint of which type each template is to be applied to. Let’s look at a modified version of the aboveĪs we can see we have two data templates for our two types. The TreeView control has a property called DataTemplates where we define the data templates for our items.
The problem is we haven’t told it how to present our items. We also aren’t seeing the teams underneath each league. So that’s not exactly satisfying is it? Yeah we have our top level leagues but it obviously doesn’t know how to present it. If we run this out of the gate we get the following in our application: First, create a new project using the steps in Getting Started Let’s start with the simplest step, creating a backing data model and the corresponding TreeView Component. Please consult newer tutorials and documentation for how to use this against current releases of Avalonia. NOTE: This article is from 2019 and Avalonia has been under active development and improvement since that time. You can find the final solution for this blog post in this Gitlab Repository So screenshots and instructions will be from there but this obviously works with any editor. As an FYI, I’m doing my work in JetBrain’s Rider IDE This is based on a similar tutorial written for WPF by Mike Hillberg that you can find hereĮxcept that we are skipping the manual list aspects of it. In this tutorial we are going to go over creating a TreeView control for a league roster system. Tree Views are a standard control for looking at hierarchal data in a user interface.