Как решать это в значительной степени зависит от того, относится ли сортировкой к (а) тип представления или (b) к экземпляру представления. Первое будет иметь место, если вы только хотели бы указать, что, например, виды типа ViewA
должны быть выше видов типа ViewB
. Последнее относится к случаю, если вы хотите указать, как сортируются несколько конкретных экземпляров одного вида вида.
A. Тип сортировки мудрая
В опции для реализации пользовательского атрибута, что-то вроде OrderIndexAttribute
, которое обнажает целочисленное свойство:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public OrderIndexAttribute : Attribute
{
public int Index { get; }
public OrderIndexAttribute(int index)
{
Index = index;
}
}
Марк ваш вид класса с этим атрибутом:
[OrderIndex(2)]
public ViewA : UserControl
{...}
Получить атрибут типа при добавлении вида в регион:
case NotifyCollectionChangedAction.Add:
foreach (FrameworkElement element in e.NewItems)
{
// Get index for view
var viewType = element.GetType();
var viewIndex= viewType.GetCustomAttribute<OrderIndexAttribute>().Index;
// This method needs to iterate through the views in the region and determine
// where a view with the specified index needs to be inserted
var insertionIndex = GetInsertionIndex(viewIndex);
regionTarget.Children.Insert(insertionIndex, element);
}
break;
B. Экземпляр Сортировка мудрая
Сделайте вид реализации интерфейса:
public interface ISortedView
{
int Index { get; }
}
Добавляя мнение в регионе, попробуйте литья вставленный вид интерфейса, прочитайте индекс и затем сделать то же самое, что и выше:
case NotifyCollectionChangedAction.Add:
foreach (FrameworkElement element in e.NewItems)
{
// Get index for view
var sortedView = element as ISortedView;
if (sortedView != null)
{
var viewIndex = sortedView.Index;
// This method needs to iterate through the views in the region and determine
// where a view with the specified index needs to be inserted
var insertionIndex = GetInsertionIndex(viewIndex);
regionTarget.Children.Insert(insertionIndex, sortedView);
}
else
{ // Add at the end of the StackPanel or reject adding the view to the region }
}