2013-07-14 40 views
2

我對StackPanel有RegionAdapter的以下實現,但我需要嚴格的項目我與一個區域聯繫任何人都可以幫助嗎?Custome StackPanel Prism 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; 
      } 
     }; 
    } 

回答

1

如何解決這個很大程度上取決於排序是否指的是(一)視圖的類型或(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. Sort實例明智

讓你的看法實現一個接口:

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