Event Firing Multiple Times

RogerSchlueter-7899 1,236 Reputation points

I have a UserControl that is basically just a TreeView. This Style sets the problematic event of the TreeView Items:

    TargetType="{x:Type TreeViewItem}">
    <EventSetter Event="MouseLeftButtonUp" Handler="SelectCategory" />

The event handler is in code-behind:

Private Sub SelectCategory(sender As Object, e As MouseButtonEventArgs)   'Handles MouseLeftButtonUp
    Dim c As Category = DirectCast(trvCategories.SelectedItem, Category)
End Sub

The problem is that this event is firing once for every depth level of the item. For example, suppose the Categories are like this:

Category A
    Category B
        Category C

The event fires once if Category A is clicked, twice if Category B is clicked and three times if Category C is clicked. In the latter case the Debug statement produces:

Category C
Category C
Category C

How can I correct this behavior?

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,636 questions
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
778 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114K Reputation points

    Try adding e.Handled = True to your handler.

0 additional answers

Sort by: Most helpful