Reputation: 1433
I have a WPF project where I am using MVVM Light. On a page in the project, ConnectionPage
, I have a UserControl (ctlSqlConnection
) that can raise a Connected
event. I am trying to use MVVM Light to intercept this event with EventToCommand
and execute a command called NavigateToDatabasePageCommand
on the ConnectionPage
's viewmodel (not the UserControl's own ViewModel!), but this doesn't appear to be working. That is, nothing is happening.
The DataContext
of ConnectionPage
is being set ok as the UI of the page is being populated correctly.
I know the event is being raised as I've also wired up a traditional .NET handler and this is being hit.
I am using version 5.3 of MVVM Light in case this has any bearing?
I'm new to MVVM and the toolkit, so I expect I'm doing something silly.
I've since looked at the event itself in the UserControl which is declared as
public event EventHandler<ConnectionSettingViewModel> Connected;
But when I put another non generic event handler instead like:
public event EventHandler ConnectedWithNoArgs;
this makes it work!?
So,
Here is the XAML for ConnectionPage
:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="Views.ConnectionPage"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:data="clr-namespace:Utilities.Data;assembly=Utilities"
xmlns:util="clr-namespace:Utilities.Data;assembly=Utilities"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="ConnectionPage"
DataContext="{Binding Source={StaticResource Locator}, Path=ConnectionPageViewModel}" x:Name="connPage" >
<Grid x:Name="rootGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="Create / Select Sql Server connection" Style="{StaticResource HeaderStyle}"/>
<util:SqlServerConnectionControl Grid.Row="1" x:Name="ctlSqlConnection" DataContext="{Binding SqlServerConnectionControlViewModel}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Connected">
<command:EventToCommand Command="{Binding ElementName=connPage, Path=DataContext.NavigateToDatabasePageCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</util:SqlServerConnectionControl>
</Grid>
Here is the ConnectionPageViewModel
:
public class ConnectionPageViewModel : ViewModelBase
{
SqlServerConnectionControlViewModel _serverCtrlVM;
Avalon _avalon;
IFrameNavigationService _navService;
public ConnectionPageViewModel(IFrameNavigationService navigationService, Avalon avalon)
{
_avalon = avalon;
_navService = navigationService;
_serverCtrlVM = new SqlServerConnectionControlViewModel(avalon.ConnectionStringManager);
NavigateToDatabasePageCommand = new RelayCommand(Nav);
}
private void Nav()
{
// NOT GETTING HERE!!!
_navService.NavigateTo("DatabasePage");
}
public SqlServerConnectionControlViewModel SqlServerConnectionControlViewModel
{
get { return _serverCtrlVM; }
}
public RelayCommand NavigateToDatabasePageCommand { get; private set; }
}
Upvotes: 1
Views: 248
Reputation: 1433
OK, I think I've figured it out in case this helps anyone else.
The Connected
Event is
public event EventHandler<ConnectionSettingViewModelEventArgs> Connected;
I added PassEventArgsToCommand="True"
to the EventToCommand
xaml.
I changed RelayCommand
to RelayCommand<T>
where T: EventArgs (this is the important bit!) and used ConnectionSettingViewModelEventArgs
as T.
Upvotes: 1