WPF Event happening twice.

  • Thread starter Thread starter Lloyd Sheen
  • Start date Start date
L

Lloyd Sheen

I have the following XAML code in a usercontrol (this is just a list of
folders)
:
<ListView x:Name="CDList" DockPanel.Dock="Top"
Background="Transparent" SelectionMode="Single">
<ListView.ItemTemplate >
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Button Name="CDButton" Click="CDButton_Click"
Tag="{Binding }" Background="Transparent"

..... (not relevant).

When I click the button on the page:

The code for the CDButton_Click is called just once which is expected.

The code in the event handler is :

Private Sub CDButton_Click(ByVal sender As System.Object, ByVal e As
System.Windows.RoutedEventArgs)
Dim rf As RFolder = CType(CType(sender, Button).Tag, RFolder)
RaiseEvent FolderSelected(Me, rf)
End Sub

The raiseevent is executed only once.

The code in the event handler is:

Private Sub CDList_FolderSelected(ByVal sender As Object, ByVal rf As
RFolder) Handles CDList.FolderSelected
RaiseEvent FolderSelected(Me, rf)
End Sub

If this looks wierd it is because there is a main page which hosts the
navigator controls which raise and event when clicked and then the navigator
control will pass the raised event on to the main page.

The mainwindow event handler code is (note it is executed for all the
different navigators):

Private Sub theFolderNavigator_FolderSelected(ByVal sender As Object, ByVal
rf As RFolder) Handles theFolderNavigator.FolderSelected,
ArtistNavPanel.FolderSelected, CDByDateNav.FolderSelected
Dim songs As List(Of RSongInfo)
songs = (From s In dc.RSongInfos Where s.FolderNumber = rf.FolderNumber
Select s Order By s.Album, s.TrackNumber Ascending).ToList
PopulateSongList(rf, songs)
End Sub

So the sequence of events is:

1. Click the button
2. Sub CDButton_Click is called
3. Sub CDList_FolderSelected is called (raiseevent)
4. Sub theFolderNavigator_FolderSelected is called (raiseevent) (sender
is FolderNavigator)
5. Sub theFolderNavigator_FolderSelected is called a second time (WTF?)
(sender is FolderNavigator)


This is like ASP when you can have the event happen twice if is auto fire
and you use the handles clause in VB.

It really seems to me that WPF is in the spot that ASP was with 1.0. There
are way to many things that go wrong and require workarounds.

Thanks
Lloyd Sheen
 
I have no idea if this has anything to do with your problem but I once had a
similar problem in some vb.net code. In my case, I had inadvertently added a
second event handler for the same event in one of the classes in the chain.
Since both handlers were called, the event was raised twice at the end of the
chain.
 
Re my prev post, I now remember that the way I did it was to use both

AddHandler ctrl.MyEvent, AddressOf MyEventHander

and

Private MyEventHandler (..usual stuff ) Handles ctrl.MyEvent
 
Back
Top