ionite34
11 months ago
6 changed files with 122 additions and 24 deletions
@ -0,0 +1,46 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using Avalonia.Controls; |
||||
using Avalonia.Controls.Templates; |
||||
using Avalonia.Metadata; |
||||
using JetBrains.Annotations; |
||||
using StabilityMatrix.Avalonia.Models; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Controls; |
||||
|
||||
/// <summary> |
||||
/// Selector for objects implementing <see cref="ITemplateKey{T}"/> |
||||
/// </summary> |
||||
[PublicAPI] |
||||
public class DataTemplateSelector<TKey> : IDataTemplate |
||||
where TKey : notnull |
||||
{ |
||||
/// <summary> |
||||
/// Key that is used when no other key matches |
||||
/// </summary> |
||||
public TKey? DefaultKey { get; set; } |
||||
|
||||
[Content] |
||||
public Dictionary<TKey, IDataTemplate> Templates { get; } = new(); |
||||
|
||||
public bool Match(object? data) => data is ITemplateKey<TKey>; |
||||
|
||||
/// <inheritdoc /> |
||||
public Control Build(object? data) |
||||
{ |
||||
if (data is not ITemplateKey<TKey> key) |
||||
throw new ArgumentException(null, nameof(data)); |
||||
|
||||
if (Templates.TryGetValue(key.TemplateKey, out var template)) |
||||
{ |
||||
return template.Build(data)!; |
||||
} |
||||
|
||||
if (DefaultKey is not null && Templates.TryGetValue(DefaultKey, out var defaultTemplate)) |
||||
{ |
||||
return defaultTemplate.Build(data)!; |
||||
} |
||||
|
||||
throw new ArgumentException(null, nameof(data)); |
||||
} |
||||
} |
@ -0,0 +1,11 @@
|
||||
using StabilityMatrix.Avalonia.Controls; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Models; |
||||
|
||||
/// <summary> |
||||
/// Implements a template key for <see cref="DataTemplateSelector"/> |
||||
/// </summary> |
||||
public interface ITemplateKey<out T> |
||||
{ |
||||
T TemplateKey { get; } |
||||
} |
Loading…
Reference in new issue