I have the following implementation of RegionAdapter for a StackPanel but I need strict ordering of items I associate with a region can anyone help?
I want Views that Register themselves to the Region to be able to control there position maybe an index number of some sort
protected override void Adapt(IRegion region, StackPanel regionTarget)
{
region.Views.CollectionChanged += (sender, e) =>
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (FrameworkElement element in e.NewItems)
{
regionTarget.Children.Add(element);
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (UIElement elementLoopVariable in e.OldItems)
{
var element = elementLoopVariable;
if (regionTarget.Children.Contains(element))
{
regionTarget.Children.Remove(element);
}
}
break;
}
};
}
How to tackle this greatly depends on whether the sorting refers to (a) the type of the view or (b) to the instance of the view. The former would be the case if you only wanted to specify that for example Views of type
ViewA
should be above Views of typeViewB
. The latter is the case if you want to specify how several concrete instances of the same view type are sorted.A. Sort type wise
On option is to implement a custom attribute, something like
OrderIndexAttribute
, which exposes an integer property:Mark your view class with that attribute:
Get the attribute of the type when adding the view to the region:
B. Sort instance wise
Make your views implement an interface:
On adding the view to the region, try casting the inserted view to the interface, read the index and then do the same as above: