Skip to content

Commit 18f01ae

Browse files
Merge pull request #1 from SyncfusionExamples/sample
Added the TabViewSample for the KB
2 parents c57ce8e + f1936ed commit 18f01ae

Some content is hidden

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

47 files changed

+1288
-2
lines changed

README.md

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,114 @@
1-
# How-to-set-the-BindingContext-for-SfTabItem-using-various-pages
2-
This repository contains a sample explaining how to set the BindingContext for SfTabItem using various pages.
1+
When working with the [SfTabView,](https://www.syncfusion.com/maui-controls/maui-tab-view) it's important to ensure that data binding is correctly set up so that the data is displayed within the tabs as expected. This article provides a guide on how to bind data to the respective fields within tab pages of the SfTabView.
2+
3+
**Step 1:** Create a new sample in the .NET MAUI and initialize TabView within the page with `BindingContext.`
4+
5+
**XAML**
6+
```
7+
<contentpage.bindingcontext>
8+
<local:viewmodel>
9+
</local:viewmodel></contentpage.bindingcontext>
10+
<tabview:sftabview items="{Binding Items}">
11+
```
12+
13+
**Step 2:** Create different content pages that you need to display as a `TabItem` and for each content page, set `BindingContext` from its respective ViewModel. For example,
14+
15+
**XAML**
16+
```
17+
<contentpage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:class="TabView.TabViewPage1" title="TabViewPage1" xmlns:local="clr-namespace:TabView">
18+
<contentpage.bindingcontext>
19+
<local:tabviewpage1viewmodel>
20+
</local:tabviewpage1viewmodel></contentpage.bindingcontext>
21+
<verticalstacklayout verticaloptions="Center">
22+
<label text="{Binding Title}" verticaloptions="Center" horizontaloptions="Center">
23+
</label></verticalstacklayout>
24+
</contentpage>
25+
```
26+
27+
**C#**
28+
29+
```
30+
public class TabViewPage1ViewModel
31+
{
32+
public string Title { get; set; }
33+
34+
public TabViewPage1ViewModel()
35+
{
36+
Title = "Welcome to .NET MAUI!";
37+
}
38+
}
39+
```
40+
41+
Create as many content pages as you need for display within the `TabItem` and its respective view model, like above.
42+
43+
**Step 3:** To bind data to the tab pages, you need to set up your ViewModel to manage the data for each tab. Here's an example of how you can initialize your tab items and bind the context to each page:
44+
45+
**C#**
46+
```csharp
47+
// ViewModel.cs
48+
49+
public class ViewModel : INotifyPropertyChanged
50+
{
51+
private TabItemCollection items;
52+
public event PropertyChangedEventHandler PropertyChanged;
53+
public TabItemCollection Items
54+
{
55+
get { return items; }
56+
set
57+
{
58+
items = value;
59+
OnPropertyChanged("Items");
60+
}
61+
}
62+
63+
protected virtual void OnPropertyChanged(string propertyName)
64+
{
65+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
66+
}
67+
68+
public ViewModel()
69+
{
70+
SetItems();
71+
}
72+
73+
internal void SetItems()
74+
{
75+
Items = new TabItemCollection();
76+
TabViewPage1 page1 = new TabViewPage1();
77+
TabViewPage2 page2 = new TabViewPage2();
78+
TabViewPage3 page3 = new TabViewPage3();
79+
TabViewPage3 page4 = new TabViewPage3();
80+
SfTabItem firstTabitem = new SfTabItem{ Content = page1.Content, Header = "Page1" };
81+
firstTabitem.Content.BindingContext = page1.BindingContext;
82+
83+
SfTabItem secondTabitem = new SfTabItem { Content = page2.Content, Header = "Page2" };
84+
secondTabitem.Content.BindingContext = page2.BindingContext;
85+
86+
SfTabItem thirdTabitem = new SfTabItem { Content = page3.Content, Header = "Page3" };
87+
thirdTabitem.Content.BindingContext = page3.BindingContext;
88+
89+
SfTabItem fourthTabitem = new SfTabItem { Content = page4.Content, Header = "Page4" };
90+
fourthTabitem.Content.BindingContext = page4.BindingContext;
91+
92+
Items.Add(firstTabitem);
93+
Items.Add(secondTabitem);
94+
Items.Add(thirdTabitem);
95+
Items.Add(fourthTabitem);
96+
97+
}
98+
}
99+
```
100+
101+
In this example, each `SfTabItem` is initialized with the content and header, and the `BindingContext` of the content is set to the `BindingContext` of the respective page.
102+
103+
Please note that the code snippets provided in this article are for illustrative purposes and may require adjustments to fit the specific requirements of your application.
104+
105+
**Output**
106+
107+
![ezgif.com-video-to-gif (9).gif](https://support.syncfusion.com/kb/agent/attachment/article/14410/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjE0ODg2Iiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5zeW5jZnVzaW9uLmNvbSJ9.VeZIlJvk_lBwkDgAMxLxp2Uql6ap6gzG1mjYNNBF4_8)
108+
109+
110+
:::Info
111+
**Important note for prism framework**
112+
113+
If you are using the `Prism framework,` it's important to note that the SfTabView may not work as expected due to the way Prism `auto-wires` the binding context. Since the SfTabView is a `content view` control, and the content of the page is assigned to the tab item content, the page's binding context is set to null. This results in the ViewModel not being recognized when setting the page content to the tab item content. Therefore, the SfTabView may not be suitable for use with the Prism framework.
114+
:::

TabViewSample/TabViewSample.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.34309.116
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TabViewSample", "TabViewSample\TabViewSample.csproj", "{B2AF12D9-A546-4C4C-B5F0-DD3635139965}"
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+
{B2AF12D9-A546-4C4C-B5F0-DD3635139965}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B2AF12D9-A546-4C4C-B5F0-DD3635139965}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B2AF12D9-A546-4C4C-B5F0-DD3635139965}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{B2AF12D9-A546-4C4C-B5F0-DD3635139965}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{B2AF12D9-A546-4C4C-B5F0-DD3635139965}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{B2AF12D9-A546-4C4C-B5F0-DD3635139965}.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 = {314A4CC8-7A06-4BD0-B4BC-76A65707C1D1}
26+
EndGlobalSection
27+
EndGlobal
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:TabViewSample"
5+
x:Class="TabViewSample.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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace TabViewSample
2+
{
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
9+
MainPage = new AppShell();
10+
}
11+
}
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="TabViewSample.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:TabViewSample"
7+
Shell.FlyoutBehavior="Disabled"
8+
Title="TabViewSample">
9+
10+
<ShellContent
11+
Title="Home"
12+
ContentTemplate="{DataTemplate local:MainPage}"
13+
Route="MainPage" />
14+
15+
</Shell>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace TabViewSample
2+
{
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
10+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Syncfusion.Maui.Core.Hosting;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace TabViewSample
5+
{
6+
public static class MauiProgram
7+
{
8+
public static MauiApp CreateMauiApp()
9+
{
10+
var builder = MauiApp.CreateBuilder();
11+
builder
12+
.UseMauiApp<App>()
13+
.ConfigureSyncfusionCore()
14+
.ConfigureFonts(fonts =>
15+
{
16+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
17+
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
18+
});
19+
20+
#if DEBUG
21+
builder.Logging.AddDebug();
22+
#endif
23+
24+
return builder.Build();
25+
}
26+
}
27+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
4+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
</manifest>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Android.App;
2+
using Android.Content.PM;
3+
using Android.OS;
4+
5+
namespace TabViewSample
6+
{
7+
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
8+
public class MainActivity : MauiAppCompatActivity
9+
{
10+
}
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Android.App;
2+
using Android.Runtime;
3+
4+
namespace TabViewSample
5+
{
6+
[Application]
7+
public class MainApplication : MauiApplication
8+
{
9+
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
10+
: base(handle, ownership)
11+
{
12+
}
13+
14+
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
15+
}
16+
}

0 commit comments

Comments
 (0)