2011-07-06 76 views
0
<sdk:TreeView x:Name="tvPageManager" SelectedItemChanged="tvPageManager_SelectedItemChanged" 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}" MouseLeftButtonDown="imgPageIcon_MouseLeftButtonDown" 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}" TextChanged="tbPageName_TextChanged" /> 
           </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}" MouseLeftButtonDown="imgMoveUp_MouseLeftButtonDown" Tag="{Binding KeyPointId}"/> 
               <Image x:Name="imgMoveDn" Source="../Assets/Images/down_arrow.png" Style="{StaticResource MoveIconsStyle}" MouseLeftButtonDown="imgMoveDn_MouseLeftButtonDown" Tag="{Binding KeyPointId}"/> 
              </StackPanel> 
             </StackPanel> 
            </DataTemplate> 
           </sdk:HierarchicalDataTemplate.ItemTemplate> 
          </sdk:HierarchicalDataTemplate> 
         </sdk:TreeView.ItemTemplate> 
        </sdk:TreeView>  

對於上面的XAML,我需要以編程方式知道,這是選定的節點還是葉?Silverlight 4:TreeView/HierarchicalDataTemplate /獲取選定的節點/葉問題

回答

1

所以當前節點作爲參數(e.NewValue)通過您已經捕捉評選活動「tvPageManager_ SelectedItemChanged」:

private void tvPageManager_SelectionChanged(object sender, RoutedPropertyChangedEventArgs<Object> e) 
{ 
    //Perform actions when SelectedItem changes 
    MessageBox.Show(((TreeViewItem) e.NewValue).Header.ToString()); 
} 
+0

對不起,不夠具體。其實我需要從c#代碼中選擇一個節點。 場景:我在運行時插入/刪除子節點,此時集合發生更改(樹視圖已綁定到此; this.tvPageManager.ItemsSource = burst.BurstPages)。因此,選擇(選定焦點)在樹視圖中更改。我需要在添加/刪除子節點成功完成後再次設置它。 –

+0

啊,你想編寫一個遞歸搜索,然後在所需的樹節點上將IsSelected屬性設置爲true。識別目標節點的方法是什麼?標記值?文本? –

0

高科技型魔法, 我對沒有充分解釋我的問題很抱歉,我已經找到了解決辦法;

private void ExpandNodeAndSelectChild() 
    { 
     if (branchSelector < 1 || leafSelector < 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; 

       foreach (var leaf in item.Items) 
       { 
        item = (this.tvName.GetContainerFromItem(leaf as Model.LeafBusinessObject) as TreeViewItem); 
        if (item != null && ((leaf as Model.LeafBusinessObject).Id== leafSelector)) 
        { 
         item.IsSelected = true; 
         break; 
        }       
       } 
      } 
      itemAtIndex++; 
     } 
    }