2014-06-12 64 views
0

我在WPF中顯示樹狀視圖,如下所示。從WPF樹狀視圖中獲取選定文本

<TreeView Height="332" Background="Transparent" BorderBrush="White" HorizontalAlignment="Left" Margin="0,46,0,0" Name="treeView1" VerticalAlignment="Top" Width="182" MouseDoubleClick="treeView1_MouseDoubleClick" SelectedItemChanged="treeView1_SelectedItemChanged"> 

     <TreeViewItem > 
      <TreeViewItem.Header> 
       <StackPanel Orientation="Horizontal"> 
        <Image Source="../Images/Logos/Gway.png" Height="24" Width="24"></Image> 
        <Label Content="Gway"></Label> 
       </StackPanel> 
      </TreeViewItem.Header> 

      <!-- Child Item --> 

      <TreeViewItem> 
       <TreeViewItem.Header> 
        <StackPanel Orientation="Horizontal"> 
         <Image Source="../Images/Logos/Scan.png" Height="24" Width="24"></Image> 
         <Label Content="Scan"></Label>       
        </StackPanel>      
       </TreeViewItem.Header> 
      </TreeViewItem> 

      <TreeViewItem> 
       <TreeViewItem.Header> 
        <StackPanel Orientation="Horizontal"> 
         <Image Source="../Images/Logos/Configuration.png" Height="24" Width="24"></Image> 
         <Label Content="Config"></Label> 
        </StackPanel> 
       </TreeViewItem.Header> 
      </TreeViewItem> 

     </TreeViewItem> 


     <TreeViewItem > 
      <TreeViewItem.Header> 
       <StackPanel Orientation="Horizontal"> 
        <Image Source="../Images/Logos/Fiware_1.png" Height="24" Width="24"></Image> 
        <Label Content="Fi ware"></Label> 
       </StackPanel> 
      </TreeViewItem.Header>    

     </TreeViewItem> 

     <TreeViewItem > 
      <TreeViewItem.Header> 
       <StackPanel Orientation="Horizontal"> 
        <Image Source="../Images/Logos/data.png" Height="24" Width="24"></Image> 
        <Label Content="Request"></Label> 
       </StackPanel> 
      </TreeViewItem.Header> 

      <!-- Child Item --> 

      <TreeViewItem> 
       <TreeViewItem.Header> 
        <StackPanel Orientation="Horizontal"> 
         <Image Source="../Images/Logos/assign.png" Height="24" Width="24"></Image> 
         <Label Content="Assign"></Label> 
        </StackPanel> 
       </TreeViewItem.Header> 
      </TreeViewItem> 

      <TreeViewItem> 
       <TreeViewItem.Header> 
        <StackPanel Orientation="Horizontal"> 
         <Image Source="../Images/Logos/update.png" Height="24" Width="24"></Image> 
         <Label Content="Update"></Label> 
        </StackPanel> 
       </TreeViewItem.Header> 
      </TreeViewItem> 

      <TreeViewItem> 
       <TreeViewItem.Header> 
        <StackPanel Orientation="Horizontal"> 
         <Image Source="../Images/Logos/UnAssign.png" Height="24" Width="24"></Image> 
         <Label Content="Unassign"></Label> 
        </StackPanel> 
       </TreeViewItem.Header> 
      </TreeViewItem> 

      <TreeViewItem> 
       <TreeViewItem.Header> 
        <StackPanel Orientation="Horizontal"> 
         <Image Source="../Images/Logos/Delete.png" Height="24" Width="24"></Image> 
         <Label Content="Delete"></Label> 
        </StackPanel> 
       </TreeViewItem.Header> 
      </TreeViewItem> 

     </TreeViewItem> 

    </TreeView> 

並嘗試處理節點上的雙擊事件。但無法從事件參數/發件人獲取選定的文本/值(這裏是Gway,Scan等..)。以下是事件處理程序代碼。

private void treeView1_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
    { 
     object s = e.Source; 

     TreeView obj = (TreeView) s; 

     TreeViewItem item2 =(TreeViewItem)obj.SelectedItem;// item2 doesnt have any selctedText/ value property 


    } 

請幫我一把。

+0

你想獲得標籤和圖像值? – Sajeetharan

回答

0

既然是它有一個樹下所以它不只是一個簡單的文本,所以爲了得到的是那裏你可以參考以下,並使用適合您需要的任何方法樹型視圖項目

爲標題的TreeViewItem的
  • Header屬性顯示有(只有一套)
  • ItemsSource屬性檢索勢必低於
  • 的DataContext檢索綁定了(它轉換爲相應的類型)的數據收集和檢索您所需值。