Abbas Hamza

Abbas Hamza

  • NA
  • 100
  • 51.1k

can i display 2 different viewModels in a singl wpf windo

Apr 6 2014 5:31 PM
Hi Guysm
i have 2 different viewModels and i want to display them in a single WPF windows any ideas if that is posible as all my code for one Viewmodel is displayed Xaml and this line of code behind for main window 
public partial class MainWindow : Window
{
public EscortViewModel eViewModel { private set; get; }
public MainWindow()
{
InitializeComponent();
eViewModel = new EscortViewModel();
eViewModel.LoadCustomer();
DataContext = eViewModel;
  
}
}
 
the other issue i have I'm using EditCommand  class which implement  ICommand.
is it possible to use the same class for both view Model even if i decided to use seprate wpf window for each model
here is my MainWindow XAML
 
<Window x:Class="WpfGlobalReach.Views.MainWindow" Name="This"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:WpfGlobalReach.ViewModels"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance viewModels:EscortViewModel}"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="687.552">
<StackPanel>
<ListView Name="lsvEscort" HorizontalAlignment="Left" Height="91" Margin="10,93,0,0" VerticalAlignment="Top" Width="642"
ItemsSource="{Binding Escorts}"
DisplayMemberPath="FirstName"
SelectedItem="{Binding CurrentEscort, Mode=TwoWay}"
SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding EscortId }" Header="EscortID" Width="auto" />
<GridViewColumn DisplayMemberBinding="{Binding FirstName}" Header="FirstName" Width="82" />
<GridViewColumn DisplayMemberBinding="{Binding AddressId}" Header="AddressId" Width="82" />
</GridView>
</ListView.View>
</ListView>
<ListBox Name="EscortListBox"
ItemsSource="{Binding Escorts}"
DisplayMemberPath="FirstName"
SelectedItem="{Binding CurrentEscort, Mode=TwoWay}"
SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}" Height="58"></ListBox>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<Label Content="Escort Name: "></Label>
<TextBox Name="nameTextBox" Grid.Column="1"
Text="{Binding CurrentEscort.FirstName, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<Button Name="buttonSave" Grid.Row="1" Grid.Column="1"
Command="{Binding EditCommand}" >
Save</Button>
</Grid>
</StackPanel>
</Window>
thank you in advance 
 

Answers (2)