Determinng or passing node information from a mouse event in a WPF Tree
I have a WPF tree (bound to XML data). An excerpt is given below:
(Well shoot, i can't post a picture because my reputation isn't high enough yet. If my rep gets up to 10, I'll post it. Other wise just imagine a regular tree view with some plus and minus icons at a few of items on various levels)
And because I need to have special operations at certain levels, I've inserted some icons at certain levels of the tree. Well, actually I've entered them at all levels of the tree like and just just chose to show them on certain levels using triggers. This is my XAML Code for this:
<HierarchicalDataTemplate ItemsSource="{Binding Path=Elements}" x:Key="ViewEditTreeTemplate">
<StackPanel Orientation="Horizontal" Margin="1">
<Label x:Name="ElementHeaderLabel" Height="16" Padding="1" VerticalContentAlignment="Center" FontSize="11" FontWeight="Normal">
<MultiBinding Converter="{StaticResource ElementHeader}">
<Binding Path="Name.LocalName"/>
<Binding Path="Attribute[name].Value" />
</MultiBinding>
</Label>
<Image x:Name="AddButton" Source="Images/button_add_icon.png" Visibility="Collapsed" MouseLeftButtonUp="AddButton_MouseLeftButtonUp" />
<Image x:Name="DeleteButton" Source="Images/button_delete_icon.png" Visibility="Collapsed" MouseLeftButtonUp="DeleteButton_MouseLeftButtonUp" />
<Image x:Name="EditButton" Source="Images/button_edit_icon.png" Visibility="Collapsed" MouseLeftButtonUp="EditButton_MouseLeftButtonUp"/>
</StackPanel>
<HierarchicalDat开发者_如何学PythonaTemplate.Triggers>
<!-- Showing aditional buttons for RFFs list -->
<DataTrigger Binding="{Binding Path=Name.LocalName}" Value="RFFs">
<Setter TargetName="AddButton" Property="Visibility" Value="Visible"/>
<Setter TargetName="DeleteButton" Property="Visibility" Value="Visible"/>
</DataTrigger>
<!-- Showing aditional buttons for RFF -->
<DataTrigger Binding="{Binding Path=Name.LocalName}" Value="RFF">
<Setter TargetName="EditButton" Property="Visibility" Value="Visible"/>
</DataTrigger>
<!-- Showing aditional buttons for Stations list -->
<DataTrigger Binding="{Binding Path=Name.LocalName}" Value="Stations">
<Setter TargetName="AddButton" Property="Visibility" Value="Visible"/>
<Setter TargetName="DeleteButton" Property="Visibility" Value="Visible"/>
</DataTrigger>
<!-- Showing aditional buttons for AdjacentRegionNames list -->
<DataTrigger Binding="{Binding Path=Name.LocalName}" Value="AdjacentRegionNames">
<Setter TargetName="AddButton" Property="Visibility" Value="Visible"/>
<Setter TargetName="DeleteButton" Property="Visibility" Value="Visible"/>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
My problem is this. I've added an event handler to those icons (AddButton, DeleteButton, and EditButton), but when I get inside the event handling routine, I have no idea which node the user was on when they clicked on the node. Can i pass anything to that event handler MouseLeftButtonUp(...) or is there some way i can figure out where the user was when they clicked my image?
The TreeViewItem which the user clicked is accessible through the event args:
private void TreeView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
TreeViewItem ti = (TreeViewItem)e.Source;
//do stuff here
}
If the item is not actually a "TreeViewItem" you should be able to cast it to it's type in order to work with it.
精彩评论