I have a MvxContentPage with a CollectionView. The items in the CollectionView are to be presented with a MvxContentView. I've tried and tried but can't get the MvxContentView to load it's associated ViewModel. I've downloaded the MvvmCross code and looked at the samples. There is an example in the PlayGround project but it seems to be incomplete.
The page below shows the content of a Set. A Set contains n SetItems which should be shown in the CollectionView. Each SetItem is presented using the partial view.
Here's my code:
The MvxContentPage:
<?xml version="1.0" encoding="utf-8" ?>
<mvx:MvxContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:mvx="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
xmlns:views="clr-namespace:eTabber.Views"
xmlns:model="clr-namespace:eTabber.Data.Model;assembly=eTabber.Data"
xmlns:viewmodels="clr-namespace:eTabber.Core.ViewModels;assembly=eTabber.Core"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Padding="0"
x:Class="eTabber.Views.SetView" x:TypeArguments="viewmodels:SetViewModel"
Title="{Binding PageTitle}" >
<StackLayout
StyleClass="MainContainer"
Spacing="0"
Margin="0"
Padding="0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Text="{Binding SetList.Name, Mode=OneWay}" StyleClass="Title" Grid.Row="0"></Label>
<StackLayout Orientation="Vertical" Grid.Row="1">
<Button Text="Add" Command="{Binding AddCommand}" />
<CollectionView ItemsSource="{Binding SetItems}"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
x:Name="SetlistsListView"
Grid.Row="2"
SelectionMode="Single"
ChildrenReordered="SetlistsListView_ChildrenReordered">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:SetItem" x:Name="DataTemplate">
<views:SetItemView BindingContext="{Binding Source={x:Reference DataTemplate}}" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</Grid>
</StackLayout>
</mvx:MvxContentPage>
And the partial view with MvxContentView:
<?xml version="1.0" encoding="UTF-8"?>
<mvx:MvxContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:mvx="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodels="clr-namespace:eTabber.Core.ViewModels;assembly=eTabber.Core"
x:TypeArguments="viewmodels:SetItemViewModel"
x:Class="eTabber.Views.SetItemView">
<ContentView.Content>
<Grid x:Name="SetlistsInnerView" RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Text="{Binding Set.SetName, Mode=OneWay}"
IsVisible="{Binding TitleVisible, Mode=OneWay}"
StyleClass="Title"
Grid.Row="0">
</Label>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Label Text="{Binding Path=Song.Artist.Name, Mode=OneWay}" Grid.Column="0" Grid.Row="0" StyleClass="Label"/>
<Label Text="{Binding Path=Song.Title, Mode=OneWay}" Grid.Column="1" Grid.Row="0" StyleClass="Label"/>
</Grid>
</Grid>
</ContentView.Content>
</mvx:MvxContentView>
Code behind:
using eTabber.Core.ViewModels;
using MvvmCross.Forms.Views;
using Xamarin.Forms.Xaml;
namespace eTabber.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SetItemView : MvxContentView<SetItemViewModel>
{
public SetItemView()
{
InitializeComponent();
}
}
}
And the ViewModel:
using eTabber.Data.Model;
using MvvmCross.Navigation;
using Xamarin.Forms;
namespace eTabber.Core.ViewModels
{
public class SetItemViewModel : BaseViewModel<SetItem>
{
public SetItem SetItem { get => passedEntity; set => passedEntity = value; }
public SetItemViewModel(IMvxNavigationService navigationService) : base(navigationService)
{
var context = View.BindingContextProperty;
}
}
}
It does show the page with the information so some binding is taking place but I want it to load the ViewModel (in which case the bindings would have to be different of course). Here's what it displays:
The issue is in the first MvxContentPage code:
<views:SetItemView BindingContext="{Binding Source={x:Reference DataTemplate}}" />
What do I specify in the Binding to get the SetItemViewModel to be loaded.
