Silverlight Custome StackPanel Prism RegionaAdapter可支持订购
我有以下针对StackPanel的RegionaAdapter实现,但我需要对与某个区域关联的项目进行严格排序,有人能帮忙吗 我希望将自己注册到该区域的视图能够控制其位置,可能是某种索引号Silverlight Custome StackPanel Prism RegionaAdapter可支持订购,silverlight,prism,silverlight-5.0,prism-4,regionadapter,Silverlight,Prism,Silverlight 5.0,Prism 4,Regionadapter,我有以下针对StackPanel的RegionaAdapter实现,但我需要对与某个区域关联的项目进行严格排序,有人能帮忙吗 我希望将自己注册到该区域的视图能够控制其位置,可能是某种索引号 protected override void Adapt(IRegion region, StackPanel regionTarget) { region.Views.CollectionChanged += (sender, e) => {
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;
}
};
}
如何解决这个问题很大程度上取决于排序是指(a)视图的类型还是(b)视图的实例。如果您只想指定例如
ViewA
类型的视图应位于ViewB
类型的视图之上,则会出现前者。如果要指定如何对同一视图类型的多个具体实例进行排序,则可以使用后者
A.按类型排序
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;
将视图添加到区域时,请尝试将插入的视图强制转换到接口,读取索引,然后执行与上面相同的操作:
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 }
}
我发现Marc的“A.排序类型”案例对我的情况非常有帮助。我需要使用OrderIndexAttribute将视图排序到区域中,如果视图实际上没有OrderIndexAttribute,我仍然能够添加视图
正如您将在下面看到的,我通过跟踪列表中的视图索引来实现这一点。没有该属性的视图的插入索引默认为零,以便排序到StackPanel的前面(或顶部)
这篇原创文章相当古老,但也许有人会像我一样偶然发现它,并会发现我的贡献很有帮助。欢迎提出重构建议。:-)
公共类StackPanelRegionaAdapter:RegionaAdapterBase
{
私有只读列表_indexList;
公共堆栈面板区域适配器(IRegionBehaviorFactory behaviorFactory)
:base(行为工厂)
{
_indexList=新列表();
}
受保护覆盖无效自适应(IRegion区域、StackPanel区域目标)
{
region.Views.CollectionChanged+=(发件人,e)=>
{
开关(电动)
{
案例NotifyCollectionChangedAction。添加:
foreach(e.NewItems中的FrameworkElement)
{
var viewType=element.GetType();
//获取此视图的自定义属性(如果有)
var customAttributes=viewType.GetCustomAttributes(false);
var viewIndex=0;//默认viewIndex为零
//确定视图是否具有OrderIndexAttribute。
//如果它确实具有OrderIndexAttribute,则获取其排序索引。
if(HasThisAttribute(customAttributes))
{
viewIndex=viewType.GetCustomAttribute().Index;
}
//获取插入索引
var insertionIndex=GetInsertionIndex(视图索引);
regionTarget.Children.Insert(insertionIndex,元素);
}
打破
案例NotifyCollectionChangedAction。删除:
foreach(e.OldItems中的UIElement elementLoopVariable)
{
var元素=elementLoopVariable;
if(regionTarget.Children.Contains(元素))
{
regionTarget.Children.Remove(元素);
}
}
打破
}
};
}
私有静态bool hastThisAttribute(IReadOnlyList customAttributes)
{
//确定视图是否具有OrderIndexAttribute
if(customAttributes.Count==0)返回false;
对于(var i=0;i
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 }
}
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();
}
}