0

Using Wpf and MVVM. I have a main window in which I happen to have an Actipro Docksite (although I believe for the overall context of this question that is probably irrelevant). The docksite has a WindowClosing event which I wish to handle but in this particular case I really need access to the event args that go with the event.

Now as I stumble my way through the steep learning curve that is WPF and MVVM I have discovered that I can use interactions to handle events like so;

 <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <i:InvokeCommandAction Command="{Binding Path=RegisterDocksiteCommand}" CommandParameter="{Binding ElementName=FtpDocksite}"/>
            </i:EventTrigger>
          <!--  <i:EventTrigger EventName="WindowClosing">
                <i:InvokeCommandAction Command="{Binding Path=DockingWindowClosingCommand}" />
            </i:EventTrigger> -->
        </i:Interaction.Triggers>

the problem with this though is that although I can catch the windowClosing event with interactions I can't see how to pass the event args as a commandParameter. Iknow that I cound handle this in the code behind but then I'd get into all sorts of grief trying to pass the evet agrs (or at least the specific event arg that I want bavk to the view model.

So I did some research and stumbled across EventManager and decided to try an experiment with it.

In the constructor of my viewmodel for the mainwindow I added the following;

EventManager.RegisterClassHandler(GetType(Docksite),DockSite.WindowClosingEvent,New RoutedEventHandler(AddressOf OnDockSiteWindowClosing))

and further down

Private Sub OnDockSiteWindowClosing(ByVal sender As Object, ByVal e As DockingWindowEventArgs)
    Select Case e.Window.Name
        Case "SchedulerViewWindow"
            SchedulerContextualTabIsVisible = False
            scheduler = Nothing
        Case Else
            Exit Select

    End Select
End Sub

Much to my pleasure this turns out to work but only if I turn Option Strict Off for the view model file. Is there any way that I can use EventManager without having to turn option Strict off?

EDIT:

the error message I get is; Option Strict On does not allow narrowing in implicit type conversions between method 'Private Sub OnDockSiteWindowClosing(sender As Object, e As ActiproSoftware.Windows.Controls.Docking.DockingWindowEventArgs)' and delegate 'Delegate Sub RoutedEventHandler(sender As Object, e As System.Windows.RoutedEventArgs)'

Thanks

Dom Sinclair
  • 2,458
  • 1
  • 30
  • 47

0 Answers0