Krogenth
Krogenth
CC#
Created by Krogenth on 8/17/2023 in #help
❔ 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/TemplatedControl that is self-referencing is appropriate for this, or if there might be some other concept I'm just unaware of.
4 replies