I am trying to implement a list of employees working in departments. There are several departments and several employees in a department. Following is my code and I have difficulty in scrolling and wrapping contents (Employee image and Name). As for wrapping the contents, if a row does not have enough space, I want the contents (image and employee's first name) to be displayed in a new line.
So far, I have tried several options but to no avail. I'm using ItemsControl
I also tried adding a StackLayout
instead of the WrapLayout
.
Can anyone please tell me how to fix the scrolling issue and content wrapping issue? Are there any workarounds or any other layouts I can use? Thank you.
XAML
<ListView ItemsSource="{Binding Departments}" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Margin="20,20,20,20">
<Label Text="{Binding DepartmentName}" />
<Label Text="{Binding DepartmentId}" />
<local:ItemsControl ItemsSource="{Binding Employees}">
<local:ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<local:WrapLayout>
<Image Source="{Binding ImageUrl}"
WidthRequest="60"
HeightRequest="60"/>
<Label
Text="{Binding FirstName}"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
LineBreakMode="WordWrap"/>
</local:WrapLayout>
</Grid>
</DataTemplate>
</local:ItemsControl.ItemTemplate>
</local:ItemsControl>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Update 1:
Classes
Department.cs
public class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
// and several other properties
public List<Employee> Employees { get; set; }
}
Employee.cs
public class Employee
{
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string ImageUrl{ get; set; }
// and several other properties
}
Update 2
Seems like the scrolling does not happen in only one of my testing devices. But still need a layout which is capable of wrapping controls.
Update 3
I want the data to be displayed as in the image below. But right now, with the above code, the contents in the WrapLayout
are not wrapped but they are resized in order to fit into one line. I want them to be wrapped if there's no space in the first line.
When you mention
WrapLayout
, I am assuming you mean this one as defined here.Also, as you are already using
ItemsControl
, I would recommend tweaking it to supportWrapLayout
instead ofStackLayout
. For example:And then change your XAML to: