This my first attempt at implementing the MVVM model so apologies in advance. I have a main window which has its own ViewModel. The Main Window then has three user controls which i which loads depending on what selected in the navigation menu. However the data context is not changing to the correct view model.
MainWindow.xaml
<Window.DataContext>
<VM:MainVM></VM:MainVM>
</Window.DataContext>
<Window.Resources>
<DataTemplate x:Key="View1Template" DataType="{x:Type VM:CustomerVM}">
<View:Customers DataContext="{Binding VM:CustomerVM}" />
</DataTemplate>
<DataTemplate x:Key="View2Template" DataType="{x:Type VM:SuppliersVM}">
<View:Suppliers DataContext="{Binding VM:Suppliers}"/>
</DataTemplate>
</Window.Resources>
<ContentControl Margin="0,135,0,10" Grid.Column="1">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource View1Template}" />
<Style.Triggers>
<DataTrigger Binding="{Binding SwitchView}" Value="0">
<Setter Property="ContentTemplate" Value="{StaticResource View1Template}" />
</DataTrigger>
<DataTrigger Binding="{Binding SwitchView}" Value="1">
<Setter Property="ContentTemplate" Value="{StaticResource View2Template}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
this has its own ViewModel and navigation loads the view correctly, however i have a command which runs on load and its not performing. This just populates a listview. I know it works because if i remove the datacontext of the window and set it as CustomerVM
the listview is populated however navigation no longer works because MainVM
has been removed.
Customer.xaml
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding LoadCustomersCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<TextBox Tag="Search Customers" x:Name="searchTextBox" Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" HorizontalAlignment="Left" Height="40" Margin="10,9,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="340" Padding="5,6,0,0" FontSize="16"/>
<Label Content="{Binding Path=SearchText}" Margin="430,0,436,263" />
<ListView ItemsSource="{Binding Customers}" x:Name="customerListBox" Margin="10,57,10,10" AlternationCount="2" >
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="Auto" DisplayMemberBinding="{Binding id}" />
<GridViewColumn Header="NAME" Width="Auto" DisplayMemberBinding="{Binding name}" />
<GridViewColumn Header="ADDRESS" Width="Auto" DisplayMemberBinding="{Binding address1}" />
<GridViewColumn Header="ADDRESS 2" Width="150" DisplayMemberBinding="{Binding address2}" />
<GridViewColumn Header="TOWN" Width="150" DisplayMemberBinding="{Binding town}" />
<GridViewColumn Header="COUNTY" Width="150" DisplayMemberBinding="{Binding county}" />
<GridViewColumn Header="POSTCODE" Width="150" DisplayMemberBinding="{Binding postcode}" />
<GridViewColumn Header="PHONE" Width="150" DisplayMemberBinding="{Binding phone}" />
<GridViewColumn Header="EMAIL" Width="150" DisplayMemberBinding="{Binding email}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
CustomerVM.cs
private string _searchText;
private readonly ObservableCollection<Customer> _customers = new ObservableCollection<Customer>();
public string Title = "Customers";
public string SearchText
{
get { return _searchText; }
set
{
_searchText = value;
RaisePropertyChangedEvent("SearchText");
}
}
public IEnumerable<Customer> Customers
{
get { return _customers; }
}
public ICommand LoadCustomersCommand
{
get { return new DelegateCommand(LoadCustomers); }
}
public void LoadCustomers()
{
Customer cus = new Customer { id = 1, name = "sam" };
_customers.Add(cus);
}
MainVM.cs
public ICommand NavigationClick
{
get { return new DelegateCommand(Navigate); }
}
public void Navigate()
{
SwitchView = 1;
}