2014-01-14 38 views
4

我有一個主窗口該主機用戶控件作爲ContentControl中host.What我想是,在第一動態地改變用戶控件上點擊按鈕(本Usercontrol)到另一個用戶控件。如何動態改變用戶控件上按鈕(點擊)存在於WPF MVVM光用戶控件

目前我已經在由各個視圖模型的用戶控件的主窗口資源

<DataTemplate DataType="{x:Type Tube:ViewModel1}" > 
     <Tube:View1/> 
</DataTemplate> 

<DataTemplate DataType="{x:Type Tube1:ViewModel2}"> 
     <Tube2:View2/> 
</DataTemplate> 

我想從視圖1更改爲view2的按鈕點擊當前創建一個DataTemplate在視圖1中。那麼我應該在ViewModel1(US1 viewModel)中做些什麼來改變US2

我目前正在開發MVVM光源。

我有一個服務定位器具有每個虛擬機的註冊實例。問題是我如何指向VM1中的VM2實例。

歡迎任何幫助!!!!!

+0

什麼是普通VM這些觀點? – Sankarann

+0

有3個VM 1主虛擬機的主窗口和2 US視圖模型爲 –

+0

@Sankarann我已經edidted問題的各個視圖.... –

回答

2

將您的Window視爲shell並使用MvvmLight的Messenger將消息發送到您的shell以交換視圖。

例如:

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfApplication1" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.DataContext> 
     <local:MainWindowViewModel></local:MainWindowViewModel> 
    </Window.DataContext> 
    <Grid> 

     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 

     <Grid.RowDefinitions> 
      <RowDefinition Height="20"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Button Grid.Row="0" Grid.Column="0" Command="{Binding ChangeFirstViewCommand}">Change View #1</Button> 
     <Button Grid.Row="0" Grid.Column="1" Command="{Binding ChangeSecondViewCommand}">Change View #2</Button> 
     <ContentControl Grid.Row="1" Grid.ColumnSpan="2" Content="{Binding ContentControlView}"></ContentControl> 
    </Grid> 
</Window> 

MainWindowViewModel.cs

using GalaSoft.MvvmLight; 
using GalaSoft.MvvmLight.Command; 
using GalaSoft.MvvmLight.Messaging; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Input; 

namespace WpfApplication1 
{ 
    public class MainWindowViewModel : ViewModelBase 
    { 
     private FrameworkElement _contentControlView; 
     public FrameworkElement ContentControlView 
     { 
      get { return _contentControlView; } 
      set 
      { 
       _contentControlView = value; 
       RaisePropertyChanged("ContentControlView"); 
      } 
     } 

     public MainWindowViewModel() 
     { 
      Messenger.Default.Register<SwitchViewMessage>(this, (switchViewMessage) => 
      { 
       SwitchView(switchViewMessage.ViewName); 
      }); 
     } 

     public ICommand ChangeFirstViewCommand 
     { 
      get 
      { 
       return new RelayCommand(() => 
       { 
        SwitchView("FirstView"); 

       }); 
      } 
     } 


     public ICommand ChangeSecondViewCommand 
     { 
      get 
      { 
       return new RelayCommand(() => 
       { 
        SwitchView("SecondView"); 
       }); 
      } 
     } 

     public void SwitchView(string viewName) 
     { 
      switch (viewName) 
      { 
       case "FirstView": 
        ContentControlView = new FirstView(); 
        ContentControlView.DataContext = new FirstViewModel() { Text = "This is the first View" }; 
        break; 

       default: 
        ContentControlView = new SecondView(); 
        ContentControlView.DataContext = new SecondViewModel() { Text = "This is the second View" }; 
        break; 
      } 
     } 
    } 
} 

FirstView.xaml

<UserControl x:Class="WpfApplication1.FirstView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <StackPanel> 
     <Label>This is the second view</Label> 
     <Label Content="{Binding Text}" /> 
     <Button Command="{Binding ChangeToSecondViewCommand}">Change to Second View</Button> 
    </StackPanel> 
</UserControl> 

FirstViewModel.cs

using GalaSoft.MvvmLight; 
using GalaSoft.MvvmLight.Command; 
using GalaSoft.MvvmLight.Messaging; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Input; 

namespace WpfApplication1 
{ 
    public class FirstViewModel : ViewModelBase 
    { 

     private string _text; 
     public string Text 
     { 
      get { return _text; } 
      set 
      { 
       _text = value; 
       RaisePropertyChanged("Text"); 
      } 
     } 

     public ICommand ChangeToSecondViewCommand 
     { 
      get 
      { 
       return new RelayCommand(() => 
       { 
        Messenger.Default.Send<SwitchViewMessage>(new SwitchViewMessage { ViewName = "SecondView" }); 
       }); 
      } 
     } 
    } 
} 

SecondView.xaml

<UserControl x:Class="WpfApplication1.SecondView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <StackPanel> 
     <Label>This is the second view</Label> 
     <Label Content="{Binding Text}" /> 
     <Button Command="{Binding ChangeToFirstViewCommand}">Change to First View</Button> 
    </StackPanel> 
</UserControl> 

SecondViewModel.cs

using GalaSoft.MvvmLight; 
using GalaSoft.MvvmLight.Command; 
using GalaSoft.MvvmLight.Messaging; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Input; 

namespace WpfApplication1 
{ 
    public class SecondViewModel : ViewModelBase 
    { 

     private string _text; 
     public string Text 
     { 
      get { return _text; } 
      set 
      { 
       _text = value; 
       RaisePropertyChanged("Text"); 
      } 
     } 

     public ICommand ChangeToFirstViewCommand 
     { 
      get 
      { 
       return new RelayCommand(() => 
       { 
        Messenger.Default.Send<SwitchViewMessage>(new SwitchViewMessage { ViewName = "FirstView" }); 
       }); 
      } 
     } 
    } 
} 

SwitchViewMessage.cs

namespace WpfApplication1 
{ 
    public class SwitchViewMessage 
    { 
     public string ViewName { get; set; } 
    } 
} 
+0

感謝好友我想通了錯誤 你使我的一天!!!!!!!!!!!!! –

1

在您的MainViewModel中使用您的vm1和vm2的基本類型創建一個屬性,例如「DisplayViewModel」。

private MyViewModelBase _displayViewModel; 

public MyViewModelBase DisplayViewModel 
{ 
    get { return _displayViewModel; } 
    set 
    { 
     _displayViewModel = value; 
     OnPropertyChanged("DisplayViewModel"); // Raise PropertyChanged 
    } 
} 

在你MainView.xaml插入結合到DisplayViewModelProperty一個ContentControl中:

<ContentControl Content="{Binding DisplayViewModel}" /> 

在您的按鈕命令,你可以用另一個視圖模型,並用您的DataTemplates組合通過二傳手修改DisplayProperty的根據新設置的ViewModel-Type,ContentControl顯示的UserControl應該更改爲View。

private void MyButtonCommand() 
{ 
    DisplayViewModel = new ViewModel2(); 
} 
+0

對不起,我誤解你的按鈕,你的主視圖模型isn't。 – thoros1179

+0

如果您MainViewModel知道兩者的ViewModels VM1和VM2,你可以在你的VM1和VM2實現事件「SwitchViewModel」和開火按鈕命令此事件。在MainViewModel你的事件處理程序,你可以再修改這將導致UI根據DataTemplate中切換到其他用戶控件的DisplayViewModel財產。 – thoros1179