Skip to content

Commit 02274ba

Browse files
Merge pull request #1 from AnanthaLakshmiKannan/master
Added KB for How to access a named ListView inside a XAML DataTemplate in .NET MAUI (SfListView)?
2 parents 6602b52 + 325160a commit 02274ba

Some content is hidden

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

42 files changed

+1303
-0
lines changed

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>

App.xaml.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace ListViewMaui
2+
{
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
9+
MainPage = new MainPage();
10+
}
11+
}
12+
}

Helper/Behavior.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using Syncfusion.Maui.ListView;
2+
using Syncfusion.Maui.Popup;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace ListViewMaui
10+
{
11+
public class GridBehavior : Behavior<Grid>
12+
{
13+
Grid grid;
14+
SfListView listView;
15+
Button button;
16+
protected override void OnAttachedTo(BindableObject bindable)
17+
{
18+
grid = bindable as Grid;
19+
grid.ChildAdded += Grid_ChildAdded;
20+
}
21+
//Method 1 : Get SfListView reference using Grid.ChildAdded Event
22+
private void Grid_ChildAdded(object sender, ElementEventArgs e)
23+
{
24+
if (e.Element is SfListView)
25+
{
26+
listView = e.Element as SfListView;
27+
listView.RefreshView();
28+
}
29+
if (e.Element is Button)
30+
{
31+
button = e.Element as Button;
32+
button.Clicked += Button_Clicked;
33+
}
34+
}
35+
//Method 2 : Get SfListView reference using FindByName
36+
private void Button_Clicked(object sender, EventArgs e)
37+
{
38+
listView = grid.FindByName<SfListView>("listView");
39+
App.Current.MainPage.DisplayAlert("Information", "ListView instance obtained", "Ok");
40+
listView.ItemTapped += ListView_ItemTapped;
41+
}
42+
43+
private void ListView_ItemTapped(object sender, Syncfusion.Maui.ListView.ItemTappedEventArgs e)
44+
{
45+
App.Current.MainPage.DisplayAlert("Information", "ListView ItemTapped", "Ok");
46+
}
47+
48+
protected override void OnDetachingFrom(BindableObject bindable)
49+
{
50+
button.Clicked -= Button_Clicked;
51+
grid.ChildAdded -= Grid_ChildAdded;
52+
listView.ItemTapped -= ListView_ItemTapped;
53+
listView = null;
54+
button = null;
55+
grid = null;
56+
base.OnDetachingFrom(bindable);
57+
}
58+
}
59+
60+
public class ContentPageBehavior : Behavior<ContentPage>
61+
{
62+
ContentPage page;
63+
SfPopup popupLayout;
64+
Button button;
65+
protected override void OnAttachedTo(ContentPage bindable)
66+
{
67+
//page = bindable;
68+
popupLayout = bindable.FindByName<SfPopup>("popupLayout");
69+
button = bindable.FindByName<Button>("ShowPopup");
70+
button.Clicked += Button_Clicked;
71+
base.OnAttachedTo(bindable);
72+
}
73+
74+
private void Button_Clicked(object sender, EventArgs e)
75+
{
76+
popupLayout.Show();
77+
}
78+
79+
protected override void OnDetachingFrom(ContentPage bindable)
80+
{
81+
button.Clicked -= Button_Clicked;
82+
base.OnDetachingFrom(bindable);
83+
}
84+
}
85+
}

Images/image_a.png

56.3 KB
Loading

Images/image_b.png

66.6 KB
Loading

Images/image_c.png

52.8 KB
Loading

Images/image_d.png

62.7 KB
Loading

Images/image_e.png

58.3 KB
Loading

ListViewMaui.csproj

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
5+
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
6+
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
7+
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
8+
9+
<!-- Note for MacCatalyst:
10+
The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64.
11+
When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>.
12+
The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated;
13+
either BOTH runtimes must be indicated or ONLY macatalyst-x64. -->
14+
<!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->
15+
16+
<OutputType>Exe</OutputType>
17+
<RootNamespace>ListViewMaui</RootNamespace>
18+
<UseMaui>true</UseMaui>
19+
<SingleProject>true</SingleProject>
20+
<ImplicitUsings>enable</ImplicitUsings>
21+
<Nullable>enable</Nullable>
22+
23+
<!-- Display name -->
24+
<ApplicationTitle>ListViewMaui</ApplicationTitle>
25+
26+
<!-- App Identifier -->
27+
<ApplicationId>com.companyname.listviewmaui</ApplicationId>
28+
29+
<!-- Versions -->
30+
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
31+
<ApplicationVersion>1</ApplicationVersion>
32+
33+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
34+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
35+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
36+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
37+
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
38+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
39+
</PropertyGroup>
40+
41+
<ItemGroup>
42+
<!-- App Icon -->
43+
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
44+
45+
<!-- Splash Screen -->
46+
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
47+
48+
<!-- Images -->
49+
<MauiImage Include="Resources\Images\*" />
50+
<MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />
51+
52+
<!-- Custom Fonts -->
53+
<MauiFont Include="Resources\Fonts\*" />
54+
55+
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
56+
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
57+
</ItemGroup>
58+
59+
<ItemGroup>
60+
<None Remove="Images\image_a.png" />
61+
<None Remove="Images\image_b.png" />
62+
<None Remove="Images\image_c.png" />
63+
<None Remove="Images\image_d.png" />
64+
<None Remove="Images\image_e.png" />
65+
</ItemGroup>
66+
67+
<ItemGroup>
68+
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
69+
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
70+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
71+
<PackageReference Include="Syncfusion.Maui.ListView" Version="*" />
72+
<PackageReference Include="Syncfusion.Maui.Popup" Version="*" />
73+
</ItemGroup>
74+
75+
<ItemGroup>
76+
<MauiImage Include="Images\image_a.png" />
77+
<MauiImage Include="Images\image_b.png" />
78+
<MauiImage Include="Images\image_c.png" />
79+
<MauiImage Include="Images\image_d.png" />
80+
<MauiImage Include="Images\image_e.png" />
81+
</ItemGroup>
82+
83+
</Project>

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.8.34330.188
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ListViewMaui", "ListViewMaui.csproj", "{3057B52E-49F9-4762-A642-5F81DCEBC106}"
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+
{3057B52E-49F9-4762-A642-5F81DCEBC106}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3057B52E-49F9-4762-A642-5F81DCEBC106}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3057B52E-49F9-4762-A642-5F81DCEBC106}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{3057B52E-49F9-4762-A642-5F81DCEBC106}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{3057B52E-49F9-4762-A642-5F81DCEBC106}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{3057B52E-49F9-4762-A642-5F81DCEBC106}.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 = {325E1898-6963-491C-A60C-C15C777F6084}
26+
EndGlobalSection
27+
EndGlobal

0 commit comments

Comments
 (0)