自定义StackPanel棱镜区域适配器,支持订购

时间:2013-07-14 09:14:18

标签: silverlight prism silverlight-5.0 prism-4 regionadapter

我为StackPanel实现了RegionAdapter的以下实现,但我需要严格排序与区域关联的项目,任何人都可以帮忙吗?

我想要将自己注册到区域的视图能够控制位置可能是某种索引编号

    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;
            }
        };
    }

2 个答案:

答案 0 :(得分:1)

如何解决这个问题很大程度上取决于排序是指(a)视图的类型还是(b)视图的实例。如果您只想指定类型为ViewA的视图应高于ViewB类型的视图,则前者就是这种情况。如果要指定如何对同一视图类型的多个具体实例进行排序,则会出现后一种情况。

:一种。明智地排序

On选项是实现一个自定义属性,类似于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 }
    }

答案 1 :(得分:0)

我发现Marc的“ A. Sort type wise”案例对我的情况非常有帮助。我需要使用OrderIndexAttribute将视图排序到该区域中,并且如果它实际上没有OrderIndexAttribute的话,仍然能够添加视图。

正如您将在下面看到的,我通过跟踪列表中的视图索​​引来做到这一点。没有该属性的视图的插入索引默认为零,以便排序到StackPanel的顶部(或顶部)。

此原始帖子比较陈旧,但也许有人会像我一样偶然发现它,并发现我的贡献会有所帮助。欢迎提供重构建议。 :-)

public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
{
    private readonly List<int> _indexList;

    public StackPanelRegionAdapter(IRegionBehaviorFactory behaviorFactory)
        : base(behaviorFactory)
    {
        _indexList = new List<int>();
    }

    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)
                    {
                        var viewType = element.GetType();

                        // Get the custom attributes for this view, if any
                        var customAttributes = viewType.GetCustomAttributes(false);

                        var viewIndex = 0;  // Default the viewIndex to zero

                        // Determine if the view has the OrderIndexAttribute.
                        // If it does have the OrderIndexAttribute, get its sort index.
                        if (HasThisAttribute(customAttributes))
                        {
                            viewIndex= viewType.GetCustomAttribute<OrderIndexAttribute>().Index;
                        }
                        
                        // Get the insertion index
                        var insertionIndex = GetInsertionIndex(viewIndex);

                        regionTarget.Children.Insert(insertionIndex, element);
                    }
                    break;

                case NotifyCollectionChangedAction.Remove:
                    foreach (UIElement elementLoopVariable in e.OldItems)
                    {
                        var element = elementLoopVariable;
                        if (regionTarget.Children.Contains(element))
                        {
                            regionTarget.Children.Remove(element);
                        }
                    }
                    break;
            }
        };
    }

    private static bool HasThisAttribute(IReadOnlyList<object> customAttributes)
    {
        // Determine if the view has the OrderIndexAttribute
        if (customAttributes.Count == 0) return false;
        for (var i = 0; i < customAttributes.Count; i++)
        {
            var name = customAttributes[i].GetType().Name;
            if (name == "OrderIndexAttribute") return true;
        }

        return false;
    }

    private int GetInsertionIndex(in int viewIndex)
    {
        // Add the viewIndex to the index list if not already there
        if (_indexList.Contains(viewIndex) == false)
        {
            _indexList.Add(viewIndex);
            _indexList.Sort();
        }

        // Return the list index of the viewIndex
        for (var i = 0; i < _indexList.Count; i++)
        {
            if (_indexList[i].Equals(viewIndex))
            {
                return i;
            }
        }

        return 0;
    }

    protected override IRegion CreateRegion()
    {
        return new AllActiveRegion();
    }
}