Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is the way I found to get the <code>selectedTreeViewItem</code> value, I got it from <code>xmlElement</code>. </p> <p>Considering this xaml code:</p> <pre><code>&lt;i&gt; &lt;Window.Resources&gt; &lt;HierarchicalDataTemplate DataType="Filial" ItemsSource="{Binding XPath=./*}"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Image Source="/WpfClient;component/Images/image4.png" Height="20"&gt;&lt;/Image&gt; &lt;TextBlock Margin="5,0,0,0" Text="{Binding XPath=@name}" FontSize="18" FontFamily="BankGothic Md BT" Foreground="#FF355CE5"/&gt; &lt;/StackPanel&gt; &lt;/HierarchicalDataTemplate&gt; &lt;HierarchicalDataTemplate DataType="Setor" ItemsSource="{Binding XPath=./*}"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Margin="5,0,0,0" Text="{Binding XPath=@name}" FontWeight="Bold" /&gt; &lt;/StackPanel&gt; &lt;/HierarchicalDataTemplate&gt; &lt;HierarchicalDataTemplate DataType="User"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Image Source="{Binding XPath=@source}" Height="15"&gt;&lt;/Image&gt; &lt;TextBlock Margin="5,0,0,0" Text="{Binding XPath=@name}" FontStyle="Italic" /&gt; &lt;/StackPanel&gt; &lt;/HierarchicalDataTemplate&gt; &lt;XmlDataProvider x:Key="xmlDP" Source="Contatos.xml" XPath="/Contatos/Filial"&gt;&lt;/XmlDataProvider&gt; &lt;/Window.Resources&gt; &lt;Grid Width="Auto" Height="Auto"&gt; &lt;TreeView Name="tv" ItemsSource="{Binding }" DataContext="{StaticResource xmlDP}" Margin="0,0,0,0" TreeViewItem.Selected="tv_Selected"&gt; &lt;TreeView.ItemContainerStyle&gt; &lt;Style TargetType="TreeViewItem"&gt; &lt;Setter Property="TreeViewItem.IsExpanded" Value="True" /&gt; &lt;/Style&gt; &lt;/TreeView.ItemContainerStyle&gt; &lt;/TreeView&gt; &lt;/Grid&gt; &lt;/i&gt; &lt;/pre&gt; </code></pre> <p>and this c# codebehind: </p> <pre><code>private void tv_Selected(object sender, RoutedEventArgs e) { System.Xml.XmlElement xmlElement= (XmlElement)tv.SelectedItem; string mySelectedValue = xmlElement.Attributes[0].Value.ToString(); MessageBox.Show(mySelectedValue , "SelectedTreeValue", MessageBoxButton.OK, MessageBoxImage.Information); } &lt;/i&gt; </code></pre> <p>and this is the xml file:</p> <pre><code>&lt;i&gt; &lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;Contatos&gt; &lt;Filial name="Firm"&gt; &lt;Setor name="Sector 1"&gt; &lt;User name="user1" source="/WpfClient;component/Images/user.png"&gt;&lt;/User&gt; &lt;User name="user2" source="/WpfClient;component/Images/user.png"&gt;&lt;/User&gt; &lt;User name="user3" source="/WpfClient;component/Images/admin.png"&gt;&lt;/User&gt; &lt;/Setor&gt; &lt;Setor name="Sector40"&gt; &lt;User name="user43" source="/WpfClient;component/Images/admin.png"&gt;&lt;/User&gt; &lt;User name="user44" source="/WpfClient;component/Images/user.png"&gt;&lt;/User&gt; &lt;User name="user45" source="/WpfClient;component/Images/user.png"&gt;&lt;/User&gt; &lt;User name="user46" source="/WpfClient;component/Images/user.png"&gt;&lt;/User&gt; &lt;/Setor&gt; &lt;/Filial&gt; &lt;/Contatos&gt; &lt;/i&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload