.net maui list view of a list with items of different subclass

I have a three classes defines:

    abstract public class PlanPosition
    {
        public long Id { get; set; }
        public Plan? Plan { get; set; } = null;
        public int Pos { get; set; }
    }

    public class AddressPos:PlanPosition
    {
        public Address Address { get; set; } 
        public LocomotionType LocomotionType { get; set; }
    }

    public class DayPos:PlanPosition
    {
        public int Day {  get; set; }
    }

I have a list of PlanPositions where the items are of AddressPos or DayPos.

Using a ListView with DataTemplate selector, i get a lot of bindings error. Here is an extract of the xaml code

    <ContentPage.Resources>
        <util:DateOnlyToDateTimeConverter x:Key="DateConverter"/>
        <DataTemplate x:Key="AddressPosTemplate"
                      x:DataType="data:AddressPos">
            <ViewCell>
                <Border HorizontalOptions="Start"
                        WidthRequest="300"
                        Padding="5"
                        Margin="20"
                        StrokeShape="RoundRectangle 10,10,10,10">

                    <VerticalStackLayout>
                        <HorizontalStackLayout Spacing="5">
                            <HorizontalStackLayout.Triggers>
                                <DataTrigger TargetType="HorizontalStackLayout" Binding="{Binding Pos}" Value="1">
                                    <Setter Property="IsVisible" Value="False"/>
                                </DataTrigger>
                            </HorizontalStackLayout.Triggers>
...
                        </HorizontalStackLayout>
                        <Label Text="{Binding Name}"
                               FontAttributes="Bold"
                               FontSize="12"/>
                    </VerticalStackLayout>
                </Border>
            </ViewCell>
        </DataTemplate>

        <DataTemplate x:Key="DayPosTemplate"
                      x:DataType="data:DayPos">
            <ViewCell>
                <Border HorizontalOptions="Start"
                        WidthRequest="100"
                        BackgroundColor="LightGray"
                        Padding="5"
                        Margin="10"
                        StrokeShape="RoundRectangle 10,10,10,10">
                    <HorizontalStackLayout HorizontalOptions="Center"
                                           Spacing="5">
                        <Image Source="menu_burger.png" HeightRequest="20" VerticalOptions="Center"/>
                        <Label Text="{Binding Day,StringFormat='Tag {0:D}'}" FontAttributes="Bold" VerticalOptions="Center"/>
                        <ImageButton x:Name="delButton" Source="trash.png" 
                                     HeightRequest="20" 
                                     IsVisible="False" 
                                     Padding="0"
                                     Margin="0"
                                     BackgroundColor="LightGray"/>
                    </HorizontalStackLayout>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup Name="CommonStates">
                            <VisualState Name="Normal">
                                <VisualState.Setters>
                                    <Setter TargetName="delButton" Property="ImageButton.IsVisible" Value="False"/>
                                    <Setter Property="WidthRequest" Value="100"/>
                                </VisualState.Setters>
                            </VisualState>
                            <VisualState Name="PointerOver">
                                <VisualState.Setters>
                                    <Setter TargetName="delButton" Property="ImageButton.IsVisible" Value="True"/>
                                    <Setter Property="WidthRequest" Value="150"/>
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Border>
            </ViewCell>
        </DataTemplate>

        <util:PlanPosDataTemplateSelector x:Key="PosSelector"
                                          AddressPos="{StaticResource AddressPosTemplate}"
                                          DayPos="{StaticResource DayPosTemplate}"/>
    </ContentPage.Resources>

The Source of the ListView is a List<PlanPosition>
The DataType of the different DataTemplates are AddressPos or DayPos.

I got errors like “Pos” Property is not found on ViewModel.

Schweregrad Anzahl  Datenkontext    Bindungspfad    Ziel    Zieltyp Beschreibung    Datei   Zeile   Projekt
Fehler  24  PlannerViewModel    Pos HorizontalStackLayout.Bound Object  'Pos' property not found on 'TripPlanner.Planner.PlannerViewModel', target property: 'Microsoft.Maui.Controls.HorizontalStackLayout.Bound'  E:NETProjectsTripplannerTripPlannerPlannerPlannerPage.xaml 23  TripPlanner

Schweregrad Anzahl  Datenkontext    Bindungspfad    Ziel    Zieltyp Beschreibung    Datei   Zeile   Projekt
Fehler  14  PlannerViewModel    Day Label.Text  String  'Day' property not found on 'TripPlanner.Planner.PlannerViewModel', target property: 'Microsoft.Maui.Controls.Label.Text'   E:NETProjectsTripplannerTripPlannerPlannerPlannerPage.xaml 52  TripPlanner

Is it not possible to display a listview with items of different classes?

I have no idea how to handle. I can create a display class that holds all attributues of all subclasses and display the approbrate property. But is not very objet oriented.

New contributor

juergjtreichlerch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật