29 lines
1.0 KiB
C#
29 lines
1.0 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Globalization;
|
|||
|
using Avalonia;
|
|||
|
using Avalonia.Controls;
|
|||
|
using Avalonia.Controls.Presenters;
|
|||
|
using Avalonia.Data.Converters;
|
|||
|
using Avalonia.Markup.Xaml.MarkupExtensions;
|
|||
|
using Avalonia.Markup.Xaml.Templates;
|
|||
|
|
|||
|
namespace Notes;
|
|||
|
|
|||
|
public class ViewModelToResourceKeyConverter: IMultiValueConverter
|
|||
|
{
|
|||
|
|
|||
|
public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
|
|||
|
{
|
|||
|
var viewModel = values[0];
|
|||
|
var controlParent = (Control?)values[1];
|
|||
|
if (viewModel == null) throw new NullReferenceException(nameof(viewModel));
|
|||
|
var name = viewModel.GetType().FullName!.Replace("ViewModel", "View");
|
|||
|
return new DynamicResourceExtension(name);
|
|||
|
object? data = null;
|
|||
|
controlParent?.TryGetResource(name, null, out data);
|
|||
|
var dataTemplate = (DataTemplate?)data;
|
|||
|
if (dataTemplate == null) throw new NullReferenceException(nameof(dataTemplate));
|
|||
|
return dataTemplate;
|
|||
|
}
|
|||
|
}
|