v.g.
v.g.

Reputation: 1076

Bind more than one ViewModel in View in XAML

I have a ViewModel class "MyViewModel" and inside I have an ObservableCollection "MyCollection".

Then in the view code-behind I do this to set the data context:

this.DataContext = new MyViewModel();

and in the front-end of the view

<Pivot ItemsSource="{Binding MyCollection}" SelectionChanged="Pivot_SelectionChanged" Margin="0" Grid.Row="1">

But what if I need to use different ViewModel "MyViewModel2" with "MyCollection2" inside in this View. How can I skip this step:

this.DataContext = new MyViewModel();

and bind only in xaml?

I tried this but without any result:

<Pivot ItemsSource="{Binding MyViewModel.MyCollection}" SelectionChanged="Pivot_SelectionChanged" Margin="0" Grid.Row="1">

Upvotes: 0

Views: 3658

Answers (2)

rPulvi
rPulvi

Reputation: 946

You'd better to use two collections in the same view model... Or you can use a master ViewModel class with reference to two sub-viewmodels; something like this:

MyViewModel class:

public class MyViewModel
{
    public ObservableCollection<string> DataInfo { get; set; }

    public MyViewModel()
    {
        DataInfo = new ObservableCollection<string>();            
    }
}

MasterViewModel class:

public class MasterViewModel
{
    public MyViewModel VM1 { get; set; }
    public MyViewModel VM2 { get; set; }

    public MasterViewModel()
    {
        this.VM1 = new MyViewModel();
        this.VM2 = new MyViewModel();

        VM1.DataInfo.Add("Data 1-1");
        VM1.DataInfo.Add("Data 1-2");

        VM2.DataInfo.Add("Data 2-1");
        VM2.DataInfo.Add("Data 2-2");
    }
}

View code-behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MasterViewModel();
    }
}

XAML

<Window x:Class="DeleteMe4SOw.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="400" Width="525">
    <StackPanel Orientation="Vertical">
        <ListView ItemsSource="{Binding VM1.DataInfo}" Height="150" Margin="10" />
        <ListView ItemsSource="{Binding VM2.DataInfo}" Height="150" Margin="10"/>
    </StackPanel>
</Window>

Upvotes: 4

v.g.
v.g.

Reputation: 1076

Here is my master class:

class CompositeViewModel
{
    public TripsResponseTypeViewModel tripsResponseTypeViewModel { get; set; }
    public TripsViewModel tripsViewModel { get; set; }
}

in TripsResponseTypeViewModel I have:

...
    private ObservableCollection<TripsResponseType> _tripsViewModelDataSource;
    public ObservableCollection<TripsResponseType> TripsViewModelDataSource
    {
        get
        {
            if (null == this._tripsViewModelDataSource)
            {
                this._tripsViewModelDataSource = new ObservableCollection<TripsResponseType>();
            }

            return this._tripsViewModelDataSource;
        }
    }
...

In my View I want to set this TripsViewModelDataSource to ItemsSource of a Pivot like this:

<Pivot ItemsSource="{Binding tripsResponseTypeViewModel.TripsViewModelDataSource}"...

In View.xaml.cs I did as you said:

this.DataContext = new CompositeViewModel();

and nothing..

Upvotes: 1

Related Questions