rahul aggarwal
rahul aggarwal

Reputation: 143

Event Names for converting into MVVM pattern

I am converting silverlight code into MVVM pattern.

I want to know event name like for "closing event" I am using code.

<i:Interaction.Triggers>
  <i:EventTrigger EventName="Closing">
      <i:InvokeCommandAction Command="{Binding OnClose}" />
  </i:EventTrigger>
</i:Interaction.Triggers>

I want to perform similar task on following events.

  1. IsVisibleChanged

  2. Windows_SizeChanged

  3. webBrowser1_Unloaded

  4. DataContextChanged

  5. LoadingProgress_Loaded

Please Help.

Upvotes: 1

Views: 811

Answers (2)

Dom Sinclair
Dom Sinclair

Reputation: 2528

Well I don't profess to be an expert in mvvm, but one way that I have found works is the following.

Let's say that I have a textbox on my form whose gotFocus and Text changed events I want to handle in my view model.

In the xaml I would have the following:

 <ribbon:TextBox x:Name="txtSubmissionSearch" Width="150"  >
                                     <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="Loaded">
                                          <i:InvokeCommandAction Command="{Binding Path=SubmissionSearchTextBoxLoadedCommand}" CommandParameter="{Binding ElementName=txtSubmissionSearch}" />
                                        </i:EventTrigger>
                                     </i:Interaction.Triggers>
                                </ribbon:TextBox>

In my viewmodel I define a private variable for the textbox

Private ersSeachSubmissionTextBox As TextBox

And then to the bit that you're really interested in:

Private _submissionSearchTextBoxLoadedCommand As ICommand
    Public ReadOnly Property SubmissionSearchTextBoxLoadedCommand As ICommand
        Get
            If _submissionSearchTextBoxLoadedCommand Is Nothing Then
                Dim mySubmissionSearchTextBoxLoaded As New Action(Of Object)(AddressOf SubmissionSearchTextBoxLoaded)
                _submissionSearchTextBoxLoadedCommand = New RelayCommand(mySubmissionSearchTextBoxLoaded)
            End If
            Return _submissionSearchTextBoxLoadedCommand
        End Get
    End Property
    Private Sub SubmissionSearchTextBoxLoaded(ByVal obj As Object)
        ersSeachSubmissionTextBox = DirectCast(obj, TextBox)
        AddHandler ersSeachSubmissionTextBox.GotFocus, AddressOf ErsSeachSubGotFocus
        AddHandler ersSeachSubmissionTextBox.TextChanged, AddressOf ErsSearchSubTextChanged
    End Sub
    Private Sub ErsSeachSubGotFocus(ByVal sender As Object, ByVal e As RoutedEventArgs)
        InvokeSubmissionEditorSearch()
    End Sub
    Private Sub ErsSearchSubTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
        InvokeSubmissionEditorSearch()
    End Sub

NB: I am using the relay command pattern as described by Josh Smith and others to handle a lot of this, but there is plenty of information available about that elsewhere and you may well be implementing it yourself anyway.

Double NB My viewModels are generally the DataContext for my Forms/UserControls. If you start to change your DataContext on the fly then you can find issues with this approach. It's not insurmountable, you just need to consider your xaml layout carefully.

Hope this helps in some way

Upvotes: 1

Mike Eason
Mike Eason

Reputation: 9713

There are no specific events which cannot be used, therefore you can simply look at the Event List in the Properties Window, or alternatively, look on MSDN.

Here is the list of events for a Window as an example.

For your Window's SizeChanged event, you'd simply do what you did before, but in the Window element.

<i:Interaction.Triggers>
    <i:EventTrigger EventName="SizeChanged">
        <i:InvokeCommandAction Command="{Binding SizeChangedCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

Upvotes: 1

Related Questions