【发布时间】:2017-06-17 01:00:09
【问题描述】:
我有一个使用 MVVM Light 的 WPF 项目。在项目的页面ConnectionPage 上,我有一个可以引发Connected 事件的UserControl (ctlSqlConnection)。我正在尝试使用 MVVM Light 通过EventToCommand 拦截此事件并在ConnectionPage 的视图模型(不是UserControl 自己的视图模型!)上执行一个名为NavigateToDatabasePageCommand 的命令,但这似乎不起作用。也就是说,什么都没有发生。
ConnectionPage 的 DataContext 设置正常,因为页面的 UI 已正确填充。
我知道该事件正在引发,因为我还连接了一个传统的 .NET 处理程序并且它正在被命中。
我正在使用 MVVM Light 5.3 版,以防万一这有任何影响?
我是 MVVM 和工具包的新手,所以我认为我在做一些傻事。
更新
我已经在声明为
的 UserControl 中查看了事件本身public event EventHandler<ConnectionSettingViewModel> Connected;
但是当我把另一个非通用事件处理程序改为:
public event EventHandler ConnectedWithNoArgs;
这让它工作了!?
所以,
- 这是否意味着交互部分无法处理通用事件声明,或者我做的不对?
- 如何将 EventArgs 从事件传递到 Nav() 方法
这是ConnectionPage 的 XAML:
<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>
这里是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; }
}
【问题讨论】:
标签: c# wpf mvvm mvvm-light eventtocommand