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.
juergjtreichlerch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2