2011-07-06 9 views
0

Silverlightアプリケーションで2つのHierarchicalDataTemplateを使用してrquired形式でデータを表示するtreeviewコントロールがあります。私はこのツリーを最初に開いたときに自動拡張したいと思っています(できればいつでも呼び出すことができるコードスニペットが望ましい)。Silverlight 4:TreeView/HierarchicalDataTemplate/AutoExpand Issue

任意のコードの代替も歓迎します。

<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のキーポイントオブジェクトを持つことがあります。

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

これは、ツリービューのようなコントロールをバインドしてコード内でどのように操作できるかについての良い点を持っています。

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