私はMVVM Lightを使用しているWPFプロジェクトを持っています。プロジェクトのページのConnectionPage
には、Connected
イベントを発生させるUserControl(ctlSqlConnection
)があります。 MVVM Lightを使用してEventToCommand
でこのイベントをインターセプトし、ConnectionPage
のviewmodel(UserControlのViewModelではなく!)のNavigateToDatabasePageCommand
というコマンドを実行しようとしていますが、これは動作していません。つまり、何も起こっていません。MVVM LightでUserControlイベントがインターセプトされない問題EventToCommand
ConnectionPage
のDataContext
は、ページのUIに正しく入力されているため、正常に設定されています。
私は伝統的な.NETハンドラを組み込み、これがヒットしているので、イベントが発生していることが分かります。
MVVM Lightのバージョン5.3が使用されていますか?
私はこれに助けていただきありがとうございます。私はMVVMとツールキットを初めて使いました。だから私は何かばかげていると思っています。
UPDATE:
public event EventHandler ConnectedWithNoArgs;
これは作る:私は以来
public event EventHandler<ConnectionSettingViewModel> Connected;
として宣言しかし、私は代わりのような他の非汎用イベントハンドラを入れたときにされたユーザーコントロールでイベント自体を見てきましたそれは働く!?
ので、
- は、それが相互作用部分は、一般的なイベントの宣言に対処することはできませんを意味しない、または私は右の何かをやっていないのですか?私はここのNav()メソッド
にイベントからのEventArgsを渡すことになるだろうどのよう
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; }
}