2011-05-26 282 views
0

我想解決一個想要構建的解決方案時遇到問題,也許這裏有人能夠引導我走向正確的方向。Silverlight 4:需要迭代幫助

我有屬於一個processflow流程,這些流程威力有孩子的,而這些孩子的威力也有兒童的等的列表。

名單如下:

ProcID  ChildOFID 
1   0 (means no child) 
2   1 
3   2 
4   3 
5   3 
6   5 

正如你可以看到PROC 「3」 包括2名兒童,其中一個(5)也有一個孩子(6)。

我想迭代這個列表並在畫布上繪製它們的對象。

現在我有下面的代碼,但它需要我寫一個我想要顯示的每個級別的循環。

int prev_location_left = 0;    
     int prev_location_top = 0; 

     // Select Last ProcessStep (has no PreID!) 
     var lastProcess = (from p in processlist 
          where p.PreID == 0 
          select p).FirstOrDefault<ProcessStep>(); 

     if (lastProcess != null) 
     { 
      create_processStep(lastProcess.ProcessID, 
           lastProcess.Name, 
           lastProcess.ProcessTypeID, 
           (900), 
           (30), 
           lastProcess.CummulativeCT, 
           lastProcess.WaitingTimeActual, 
           lastProcess.ValueAddTimeActual, 
           lastProcess.ProcessStepTime); 

      prev_location_left = 900; 
      prev_location_top = 30; 
     } 

     // Select all the ProcessSteps that are a child of the last(first) one. 
     var listChilds = (from p in processlist 
          where p.PreID == lastProcess.ProcessID 
          select p); 


     int childscount = listChilds.Count(); 
     int cnt = 0; 

     foreach (ProcessStep ps in listChilds) 
     { 
      create_processStep(ps.ProcessID, 
       ps.Name, 
       ps.ProcessTypeID, 
       (prev_location_left - (150)), 
       (30 + (60 *cnt)), 
       ps.CummulativeCT, 
       ps.WaitingTimeActual, 
       ps.ValueAddTimeActual, 
       ps.ProcessStepTime); 


      var listChilds2 = (from p in processlist 
           where p.PreID == ps.ProcessID 
           select p); 

      int cnt2 = 0; 

      foreach (ProcessStep ps2 in listChilds2) 
      { 
       create_processStep(ps2.ProcessID, 
        ps2.Name, 
        ps2.ProcessTypeID, 
        (prev_location_left - (300)), 
        (30 + (60 *cnt2)), 
        ps2.CummulativeCT, 
        ps2.WaitingTimeActual, 
        ps2.ValueAddTimeActual, 
        ps2.ProcessStepTime); 


       var listChilds3 = (from p in processlist 
            where p.PreID == ps2.ProcessID 
            select p); 

       int cnt3 = 0; 

       foreach (ProcessStep ps3 in listChilds3) 
       { 
        create_processStep(ps3.ProcessID, 
         ps3.Name, 
         ps3.ProcessTypeID, 
         (prev_location_left - (450)), 
         (30 + (60 * cnt2)), 
         ps3.CummulativeCT, 
         ps3.WaitingTimeActual, 
         ps3.ValueAddTimeActual, 
         ps3.ProcessStepTime); 
        cnt3 = cnt3 + 1; 
       } 


       cnt2 = cnt2 + 1; 
      } 


      cnt = cnt + 1; 
     } 

需要做什麼是以下幾點:

  • 獲取最後一個進程(具有PreId == 0)
  • 檢查什麼他的孩子的是,借鑑他們在畫布上:左-150,排名前30的第一個孩子,排名前90的排名第二,排名前150的排名第三,等等。

現在,對於每個發現的孩子,我還需要檢查他們是否有孩子,並再次執行相同的邏輯,我無法使這個排序無限循環。

幫助! :)

回答

1

您可以創建一個遞歸父/子對象,並與您的視圖綁定。以下是使用您提供的數據的非常基本的示例。

MainPage.xaml中

<UserControl x:Class="SilverlightApplication4.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
xmlns:local="clr-namespace:SilverlightApplication4" 
d:DesignHeight="300" d:DesignWidth="400"> 

<UserControl.DataContext> 
    <local:MainPage_ViewModel/> 
</UserControl.DataContext> 

<Canvas> 
    <local:RecursiveView DataContext="{Binding RecursiveObject}"/> 
</Canvas> 

MainPage_ViewModel.cs

public class MainPage_ViewModel 
{ 
    public MainPage_ViewModel() 
    { 
     List<KeyValuePair<int, int>> collection = new List<KeyValuePair<int, int>>() 
     { 
      new KeyValuePair<int,int>(1,0), 
      new KeyValuePair<int,int>(2,1), 
      new KeyValuePair<int,int>(3,2), 
      new KeyValuePair<int,int>(4,3), 
      new KeyValuePair<int,int>(5,3), 
      new KeyValuePair<int,int>(6,5) 
     }; 

     KeyValuePair<int, int> parent = collection.Where(kvp => kvp.Value == 0).First(); 
     collection.Remove(parent); 

     RecursiveObject recursiveObject = new RecursiveObject() 
     { 
      root = parent.Key 
     }; 

     populateChildren(recursiveObject, collection); 

     this.RecursiveObject = recursiveObject; 
    } 

    public RecursiveObject RecursiveObject 
    { 
     get { return recursiveObject; } 
     set { recursiveObject = value; } 
    } 
    private RecursiveObject recursiveObject; 


    private void populateChildren(RecursiveObject parent, List<KeyValuePair<int, int>> list) 
    { 
     List<KeyValuePair<int, int>> children = list.Where(kvp => kvp.Value == parent.root).ToList(); 
     children.ForEach(child => list.Remove(child)); 
     children.ForEach(child => 
     { 
      RecursiveObject newChild = new RecursiveObject() { root = child.Key }; 
      parent.Children.Add(newChild); 
      populateChildren(newChild, list); 
     }); 
    } 
} 

RecursiveObject.cs

public class RecursiveObject 
{ 
    public int root { get; set; } 
    public List<RecursiveObject> Children 
    { 
     get { return children; } 
     set { children = value; } 
    } 
    private List<RecursiveObject> children = new List<RecursiveObject>(); 
} 

RecursiveView.xaml

<UserControl x:Class="SilverlightApplication4.RecursiveView" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
xmlns:local="clr-namespace:SilverlightApplication4" 
d:DesignHeight="300" d:DesignWidth="400"> 

<StackPanel Margin="30,0,0,0"> 
    <TextBlock Text="{Binding root}"/> 
    <ItemsControl ItemsSource="{Binding Children}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <local:RecursiveView DataContext="{Binding}"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</StackPanel> 

圖像輸出:

enter image description here

我剛放置 '30' 的餘量上的每個的左側孩子,但你可以將它調整成任何你想要的。不知道這是否有幫助,我只是認爲這是一個有趣的挑戰:)