2011-07-06 87 views
0

我在Silverlight應用程序中有一個treeview控件,它使用2個HierarchicalDataTemplate以顯示格式顯示數據。我希望在第一次打開時自動展開此樹(最好是我可以隨時調用的代碼段)。Silverlight 4:TreeView/HierarchicalDataTemplate/AutoExpand問題

對於給定代碼的任何替代方法也將受到歡迎。

<sdk:TreeView x:Name="tvPageManager" Style="{StaticResource PageManagerStyle}"          
         ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Auto"> 
         <sdk:TreeView.ItemTemplate> 
          <sdk:HierarchicalDataTemplate ItemsSource="{Binding KeyPoints, Mode=TwoWay}"> 
           <StackPanel Orientation="Horizontal"> 
            <ToolTipService.ToolTip> 
             <ToolTip Content="{Binding PageName}" Style="{StaticResource ToolTipStyle}"/> 
            </ToolTipService.ToolTip> 
            <Image x:Name="imgPageIcon" Source="{Binding PageIconImage}" Style="{StaticResource PageIconStyle}" Tag="{Binding BurstPageId, Mode=TwoWay}" /> 
            <TextBlock x:Name="tbkLiteralTextPage" Text="Page " Style="{StaticResource PageNameLiteralTextBlockStyle}" /> 
            <TextBox x:Name="tbPageName" Text="{Binding PageName, Mode=TwoWay}" Style="{StaticResource PageNameTextBoxStyle}" /> 
           </StackPanel> 
           <sdk:HierarchicalDataTemplate.ItemTemplate> 
            <DataTemplate> 
             <StackPanel Orientation="Horizontal"> 
              <Image x:Name="imgKeypointIcon" Source="../Assets/Images/bullet_yellow.png" Style="{StaticResource KeypointIconStyle}"/> 
              <TextBlock x:Name="tbkKeypointTitle" Text="{Binding Title, Mode=TwoWay}" Style="{StaticResource KeypointNameTextBlockStyle}" /> 
              <StackPanel x:Name="spnlMoveImages" Orientation="Horizontal" HorizontalAlignment="Right" Width="30"> 
               <Image x:Name="imgMoveUp" Source="../Assets/Images/up_arrow.png" Style="{StaticResource MoveIconsStyle}" Tag="{Binding KeyPointId}"/> 
               <Image x:Name="imgMoveDn" Source="../Assets/Images/down_arrow.png" Style="{StaticResource MoveIconsStyle}" Tag="{Binding KeyPointId}"/> 
              </StackPanel> 
             </StackPanel> 
            </DataTemplate> 
           </sdk:HierarchicalDataTemplate.ItemTemplate> 
          </sdk:HierarchicalDataTemplate> 
         </sdk:TreeView.ItemTemplate> 
        </sdk:TreeView> 

此控件綁定到BurstPage類的Observable列表。完整的數據結構如下;

父元素是包含1到n個BurstPage對象的Burst對象,任何給定的BurstPage可能都有1到n個Keypoint對象。

BurstPage.Name(例如1) Keypoint.Name(比如說A) Keypoint.Name(說B) Keypoint.Name(說C) BurstPage.Name(比如2) BurstPage.Name(比方說3 ) Keypoint.Name(說d) Keypoint.Name(說E)

回答

0

我感覺有很多的代碼從您的文章丟失...

但我認爲你可能會發現以下有用:one-more-platform-difference-more-or-less-tamed

關於如何綁定控件(如treeview)並在代碼中使用它們有一些好的指針。

+0

Thanx Anders,我已經更新了XAML。請現在看看。 –

+0

好吧,我認爲你可以使用我發佈的鏈接中的方法將TreeViewItem的IsExpanded屬性綁定到綁定數據對象中的屬性。 您是否遵循MVVM模式? –

+0

我發現了一個更簡單的自動展開解決方案; ' <樣式的TargetType = 「SDK:樹型視圖」> ' –

0

你說得對,XAML只會在最初擴展樹的同時添加新的節點;

private void ExpandNode() 
{ 
    if (branchSelector < 1) 
     return; 

    TreeViewItem item = null; 
    int itemAtIndex = 0; 

    //Update tree layout 
    this.tvName.UpdateLayout(); 

    foreach (var branch in this.tvName.Items) 
    { 
     item = (this.tvName.GetContainerFromItem(this.tvName.Items[itemAtIndex]) as TreeViewItem); 
     if (item != null && item.HasItems) 
     { 
      if ((branch as Model.BranchBusinessObject).Id== branchSelector && (!item.IsExpanded)) 
       item.IsExpanded = true; 
     } 
     itemAtIndex++; 
    } 
}