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
}
請幫我一把。
你想獲得標籤和圖像值? – Sajeetharan