I am not sure if this is possible or if there are any recommended ways to achieve this. Essentially, I'm trying to have a list view with "jagged" or differing view cells foreach cell in the list view. As a super simple example, I am trying to dynamically display a button, label, and entry each in their own row of the list view. Here is my code:
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
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"
x:Class="Chaser.Views.Admin_Detail_MasterDetail_View">
<ContentPage.Content>
<StackLayout Orientation="Vertical"
Spacing="15"
Margin="10">
<Label Text="{Binding ViewTitle, Mode=OneWay}"
FontSize="Large"
TextColor="Red"
FontAttributes="Bold" />
<ListView ItemsSource="{Binding ItemViews, Mode=OneWay}"
SelectionMode="None"
PropertyChanged="ListView_PropertyChanged">
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Codebehind
public Admin_Detail_MasterDetail_View()
{
InitializeComponent();
DetailViewContext = new Models.Detail_MasterDetail_Model();
DetailViewContext.ViewTitle = "Info";
DetailViewContext.ItemViews = new List<ViewCell>
{
new ViewCell
{
View = new Button
{
Text = "TestButton"
},
},
new ViewCell
{
View = new Label
{
Text = "TestLabel"
}
},
new ViewCell
{
View = new Entry
{
Placeholder = "TestEntry"
}
}
};
BindingContext = DetailViewContext;
}
Note: This was also tried with adding stack layouts to the views then adding the individual items to the stack layouts, either way I get the following:
If possible, I would like to have command bindings on a per cellview basis as one type of cell may have differing context menu actions than another.
In the end the view cells will be much more complicated than a simple button or label, but this is for conceptual purposes to see if something like this is possible/recommended.
After some brief research I see there is a data template selector we can create, however, I am not sure if it would apply in this scenario. At the very least, I am not sure if it would allow for custom context menu actions on a per cell basis.
Thank you for any input on this!
I ended up using a data template selector to achieve this. I created custom view cells then in the data template selector, I have a data template object for reach template which gets set to the corresponding view cell.
Following This Microsoft doc, it was fairly simple to configure. This also allows me to have custom view cell context actions defined for each type of view cell.