© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
3 replies
Krogenth

❔ Avalonia nested ItemsRepeater datatemplates

I am attempting to handle rendering UI for a list of lists of objects that implement a base interface in avalonia. The issue isn't the list objects themselves, but that some implementations also hold lists of other objects of the same base type that should also be handled.
So for the base list of objects, I know I can make a selector to pick which type of implementing object to render for in the datatemplate list, i.e.:

public class InheritingObjectType : BaseInterface
{
  ...
  public List<BaseInterface> NestedObjects {get;}
};

// other implementing classes

public class SomeSelector : IDataTemplate
{
  [Content]
  public Dictionary<string, IDataTemplate> Templates { get; private set; } = new();

  public IControl Build(object data) =>
    Templates[data.GetType().Name].Build(data);

  public bool Match(object data)
    => data is BaseInterface && Templates.ContainsKey(data.GetType().Name);
}
public class InheritingObjectType : BaseInterface
{
  ...
  public List<BaseInterface> NestedObjects {get;}
};

// other implementing classes

public class SomeSelector : IDataTemplate
{
  [Content]
  public Dictionary<string, IDataTemplate> Templates { get; private set; } = new();

  public IControl Build(object data) =>
    Templates[data.GetType().Name].Build(data);

  public bool Match(object data)
    => data is BaseInterface && Templates.ContainsKey(data.GetType().Name);
}


<ItemsRepeater.DataTemplates>
  <selectors:SomeSelector>
    <DataTemplate x:Key="InheritingObjectType"
                  DataType="{x:Type types:InheritingObjectType}">
      <!-- whatever needs rendered -->
      <!-- how to render nested objects? -->
    </DataTemplate>
    <!-- other datatemplates -->
  </selectors:MapOpcodeSelector>
</ItemsRepeater.DataTemplates>
<ItemsRepeater.DataTemplates>
  <selectors:SomeSelector>
    <DataTemplate x:Key="InheritingObjectType"
                  DataType="{x:Type types:InheritingObjectType}">
      <!-- whatever needs rendered -->
      <!-- how to render nested objects? -->
    </DataTemplate>
    <!-- other datatemplates -->
  </selectors:MapOpcodeSelector>
</ItemsRepeater.DataTemplates>


I'm unsure if just defining some
UserControl
UserControl
/
TemplatedControl
TemplatedControl
that is self-referencing is appropriate for this, or if there might be some other concept I'm just unaware of.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Accessing Function from Nested UI File - Avalonia
C#CC# / help
2y ago
✅ How to get value of DatePicker from nested UserControl Avalonia
C#CC# / help
2y ago
Avalonia Issue
C#CC# / help
10mo ago
Avalonia MVVM
C#CC# / help
2y ago