Skip to content

Commit f11d9ff

Browse files
committed
MAUI-3289 To add an item in a specific index with groups
1 parent 7f22eb1 commit f11d9ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1597
-0
lines changed

ListViewMaui/ListViewMaui.sln

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31611.283
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ListViewMaui", "ListViewMaui\ListViewMaui.csproj", "{02F7D986-E626-42A6-A9A1-B36C96C4290B}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{02F7D986-E626-42A6-A9A1-B36C96C4290B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{02F7D986-E626-42A6-A9A1-B36C96C4290B}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{02F7D986-E626-42A6-A9A1-B36C96C4290B}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{02F7D986-E626-42A6-A9A1-B36C96C4290B}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{02F7D986-E626-42A6-A9A1-B36C96C4290B}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{02F7D986-E626-42A6-A9A1-B36C96C4290B}.Release|Any CPU.Deploy.0 = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(SolutionProperties) = preSolution
22+
HideSolutionNode = FALSE
23+
EndGlobalSection
24+
GlobalSection(ExtensibilityGlobals) = postSolution
25+
SolutionGuid = {61F7FB11-1E47-470C-91E2-47F8143E1572}
26+
EndGlobalSection
27+
EndGlobal

ListViewMaui/ListViewMaui/App.xaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:ListViewMaui"
5+
x:Class="ListViewMaui.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace ListViewMaui;
2+
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
9+
MainPage = new MainPage();
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="ListViewMaui.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:ListViewMaui"
7+
Shell.FlyoutBehavior="Disabled">
8+
9+
<ShellContent
10+
Title="Home"
11+
ContentTemplate="{DataTemplate local:MainPage}"
12+
Route="MainPage" />
13+
14+
</Shell>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ListViewMaui;
2+
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using Microsoft.Maui.Controls;
2+
using Syncfusion.Maui.DataSource;
3+
using Syncfusion.Maui.DataSource.Extensions;
4+
using Syncfusion.Maui.ListView;
5+
using Syncfusion.Maui.ListView.Helpers;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Reflection;
10+
using System.Text;
11+
using System.Threading.Tasks;
12+
13+
namespace ListViewMaui
14+
{
15+
public class Behavior:Behavior<ContentPage>
16+
{
17+
#region Fields
18+
19+
private Syncfusion.Maui.ListView.SfListView listView;
20+
private Button Addbutton;
21+
private ContactsViewModel viewModel;
22+
GroupResult itemGroup;
23+
24+
#endregion
25+
26+
#region Overrides
27+
protected override void OnAttachedTo(ContentPage bindable)
28+
{
29+
listView = bindable.FindByName<Syncfusion.Maui.ListView.SfListView>("listView");
30+
31+
viewModel = new ContactsViewModel();
32+
listView.BindingContext = viewModel;
33+
listView.ItemsSource = viewModel.ContactItems;
34+
35+
listView.DataSource.GroupDescriptors.Add(new GroupDescriptor()
36+
{
37+
PropertyName = "ContactName",
38+
KeySelector = (object obj1) =>
39+
{
40+
var item = (obj1 as Contact);
41+
return item.ContactName[0].ToString();
42+
}
43+
});
44+
45+
46+
Addbutton = bindable.FindByName<Button>("addItem");
47+
Addbutton.Clicked += Addbutton_Clicked;
48+
base.OnAttachedTo(bindable);
49+
}
50+
51+
52+
#region CallBacks
53+
54+
private void Addbutton_Clicked(object sender, EventArgs e)
55+
{
56+
var contact = new Contact();
57+
contact.ContactName = "Adam";
58+
contact.ContactNumber = "783-457-567";
59+
contact.ContactImage = "image" + 25 + ".png";
60+
viewModel.ContactItems.Add(contact);
61+
62+
GetGroupResult(contact);
63+
64+
if (itemGroup == null)
65+
return;
66+
67+
var items = itemGroup.GetType().GetRuntimeProperties().FirstOrDefault(x => x.Name == "ItemList").GetValue(itemGroup) as List<object>;
68+
InsertItemInGroup(items, contact, 0);
69+
}
70+
71+
#endregion
72+
73+
#region Methods
74+
75+
internal void GetGroupResult(object ItemData)
76+
{
77+
var descriptor = listView.DataSource.GroupDescriptors[0];
78+
object key;
79+
80+
if (descriptor.KeySelector == null)
81+
{
82+
var propertyInfoCollection = new PropertyInfoCollection(ItemData.GetType());
83+
key = propertyInfoCollection.GetValue(ItemData, descriptor.PropertyName);
84+
}
85+
else
86+
key = descriptor.KeySelector(ItemData);
87+
88+
for (int i = 0; i < this.listView.DataSource.Groups.Count; i++)
89+
{
90+
var group = this.listView.DataSource.Groups[i];
91+
if ((group.Key != null && group.Key.Equals(key)) || group.Key == key)
92+
{
93+
itemGroup = group;
94+
break;
95+
}
96+
group = null;
97+
}
98+
descriptor = null;
99+
key = null;
100+
}
101+
102+
internal void InsertItemInGroup(List<object> items, object Item, int InsertAt)
103+
{
104+
items.Remove(Item);
105+
items.Insert(InsertAt, Item);
106+
}
107+
#endregion
108+
protected override void OnDetachingFrom(ContentPage bindable)
109+
{
110+
listView = null;
111+
Addbutton.Clicked -= Addbutton_Clicked;
112+
Addbutton = null;
113+
viewModel = null;
114+
base.OnDetachingFrom(bindable);
115+
}
116+
#endregion
117+
}
118+
}
56.3 KB
Loading
66.6 KB
Loading
52.8 KB
Loading
62.7 KB
Loading

0 commit comments

Comments
 (0)