0

As the title said, I tried to get data from 2 different ViewModel but it shows nothing on it. Am I doing it wrong?
Here is my XAML

<Window.DataContext> <local:VMContainer/> </Window.DataContext> <Grid> <StackPanel Orientation="Horizontal"> <ItemsControl ItemsSource="{Binding Path=VM1.LibraryVM1}"> <ItemsControl.ItemTemplate> <DataTemplate> <Image Width="150" Height="200" Source="{Binding Path=cover}" /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> <ItemsControl ItemsSource="{Binding Path=VM2.LibraryVM2}"> <ItemsControl.ItemTemplate> <DataTemplate> <Image Width="150" Height="200" Source="{Binding Path=cover}" /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </StackPanel> </Grid> 

My Container

public class VMContainer { public VModel1 VM1 { get; set; } public VModel2 VM2 { get; set; } } 

ViewModel 1

public class VModel1 { public DataView LibraryVM1 { get; private set; } public VModel1() { DataTable vm1 = new DataTable(); using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;")) { MySqlDataAdapter adapter = new MySqlDataAdapter(); adapter.SelectCommand = new MySqlCommand("Select * from index_movie_list", connection); adapter.Fill(vm1); } LibraryVM1 = vm1.DefaultView; } } 

ViewModel 2

public class VModel2 { public DataView LibraryVM2 { get; private set; } public VModel2() { DataTable vm2 = new DataTable(); using (MySqlConnection connection = new MySqlConnection("SERVER=localhost;" + "DATABASE=library;" + "UID=root;" + "PASSWORD=;")) { MySqlDataAdapter adapter = new MySqlDataAdapter(); adapter.SelectCommand = new MySqlCommand("Select * from index_tv_list", connection); adapter.Fill(vm2); } LibraryVM2 = vm2.DefaultView; } } 

when I run the program, it shows blank white only. When I change it without the container and select 1 ViewModel only it shows the image but I can't view my ViewModel 2. Are there any solutions to it?

Edit : Problem Solved after i initialize VM1 and VM2 thanks to Loocid answer.

1 Answer 1

2

Your VMContainer never initialises VModel1 and VModel2 so you're binding to null.

Try this:

public class VMContainer { public VModel1 VM1 { get; set; } = new VModel1(); public VModel2 VM2 { get; set; } = new VModel2(); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.