2011-11-04 34 views
0

我有一個主窗口來託管兩個視圖(用戶控件)。一個是UserControl1,另一個是UserControl2,在切換系統之後。使用MVVM在WPF中加載用戶控件後啓動視圖(窗口)

UserControl1先加載然後視圖可以切換。我希望在加載UserControl1後啓動一個窗口。

我使用消息傳遞模式啓動視圖並在app.xamlMainView.xaml中註冊視圖。

我該如何繼續?

的XAML我的MainView:

<Window x:Class="CustomListView.MainView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:CustomView="clr-namespace:CustomListView" 
     Title="MainView" 
     Width="300" 
     Height="300"> 
    <Window.InputBindings> 
     <CustomView:RelayKeyBinding Key="F12" CommandBinding="{Binding Path=Toggle}"></CustomView:RelayKeyBinding> 
    </Window.InputBindings> 
    <Window.Resources> 
     <DataTemplate x:Key="ExecutionView"> 
      <CustomView:ExecutionView /> 
     </DataTemplate> 
     <DataTemplate x:Key="ConfigView"> 
      <CustomView:ConfigView /> 
     </DataTemplate> 
     <Style x:Key="mainContentControlStyle" 
       TargetType="{x:Type ContentControl}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Path=Mode}" 
          Value="1"> 
        <Setter Property="ContentTemplate" Value="{StaticResource ResourceKey=ConfigView}" /> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding Path=Mode}" 
          Value="0"> 
        <Setter Property="ContentTemplate" Value="{StaticResource ResourceKey=ExecutionView}" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 
    <Grid> 
     <ContentControl Content="{Binding}" 
         Style="{StaticResource ResourceKey=mainContentControlStyle}" />   
    </Grid> 
</Window> 

如果您需要任何更多的代碼,我會很樂意將它張貼。

在此先感謝。

回答

0

您可以使用EventTrigger將窗口的Loaded事件轉換爲命令執行,然後在那裏創建窗口。這將是我的代碼:

<UserControl> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="Loaded"> 
      <i:InvokeCommandAction Command="{Binding UserControl_LoadedCommand"} /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</UserControl> 

如果您正在使用MVVM Light,這是代碼:

<UserControl> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="Loaded"> 
      <Galasoft_MvvmLight_Command:EventToCommand Command="{Binding UserControl_LoadedCommand}"/> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</UserControl> 
+0

感謝回答......將事件吩咐火....執行和CanExecute方法在ViewModel中? – Ankesh

+0

是的,他們會的。 MVVM Light提供了用於設置的文檔。 – CamronBute

+0

謝謝你的答案:) – Ankesh

相關問題