Skip to content

Commit faaea05

Browse files
Merge pull request #2 from SyncfusionExamples/Sample
Modified the sample name
2 parents a0a766d + 5e4373b commit faaea05

Some content is hidden

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

48 files changed

+104
-105
lines changed

README.md

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ This article provides a comprehensive guide on utilizing a `DataTemplateSelector
22

33
By following the steps below, you can create a flexible tab view that supports varying content for each tab based on its associated data model.
44

5-
**Step 1:** Create a CustomSfTabView
5+
**Step 1:** Create a CustomTabView
66

77
- Extend the TabView control to add a `ContentItemTemplateSelector` property.
88
- Handle the [SelectionChanged](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.TabView.SfTabView.html?tabs=tabid-1#Syncfusion_Maui_TabView_SfTabView_SelectionChanged) and `Loaded` events to dynamically set the content template.
99

1010
```
11-
public class CustomSfTabView : SfTabView
11+
public class CustomTabView : SfTabView
1212
{
1313
// Dependency property for ItemTemplateSelector
1414
public static readonly BindableProperty ContentItemTemplateSelectorProperty =
1515
BindableProperty.Create(
1616
nameof(ContentItemTemplateSelector),
1717
typeof(DataTemplateSelector),
18-
typeof(CustomSfTabView),
18+
typeof(CustomTabView),
1919
null,
2020
propertyChanged: OnContentItemTemplateSelectorChanged);
2121
@@ -29,15 +29,15 @@ public class CustomSfTabView : SfTabView
2929
// Handle changes to the template selector
3030
private static void OnContentItemTemplateSelectorChanged(BindableObject bindable, object oldValue, object newValue)
3131
{
32-
var tabView = bindable as CustomSfTabView;
32+
var tabView = bindable as CustomTabView;
3333
if (tabView != null)
3434
{
3535
tabView.SetInitialTemplate();
3636
}
3737
}
3838
3939
// Constructor
40-
public CustomSfTabView()
40+
public CustomTabView()
4141
{
4242
// Wire up selection changed event
4343
SelectionChanged += OnSelectionChanged;
@@ -77,9 +77,6 @@ public class CustomSfTabView : SfTabView
7777
7878
// Directly set the ContentItemTemplate
7979
ContentItemTemplate = selectedTemplate;
80-
81-
// Optional: Log for debugging
82-
System.Diagnostics.Debug.WriteLine($"Initial template set for item type: {firstItem?.GetType()}");
8380
}
8481
}
8582
}
@@ -114,7 +111,7 @@ public class TabItemTemplateSelector : DataTemplateSelector
114111
TextTabItem => TextTemplate,
115112
ImageTabItem => ImageTemplate,
116113
ComplexTabItem => ComplexTemplate,
117-
_ => null
114+
_ => TextTemplate
118115
};
119116
}
120117
}
@@ -143,30 +140,30 @@ public class TextTabItem:BaseTabItem
143140
- Create a view model to provide the items for the tabs and bind it to your view.
144141

145142
```
146-
public class TabViewViewModel
143+
public class TabViewModel
147144
{
148145
public List<BaseTabItem> TabItems { get; set; }
149146
150-
public TabViewViewModel()
147+
public TabViewModel()
151148
{
152149
TabItems = new List<BaseTabItem>
153150
{
154151
new TextTabItem
155152
{
156-
Title = "Text Tab",
157-
Content = "This is a text tab content"
153+
Title = "Employee",
154+
Content = "Alex"
158155
},
159156
new ImageTabItem
160157
{
161-
Title = "Image Tab",
162-
ImageSource = "dotnet_bot.png"
158+
Title = "Profile Picture",
159+
ImageSource = "user.png"
163160
},
164161
new ComplexTabItem
165162
{
166-
Title = "Complex Tab",
163+
Title = "Description",
167164
DetailedContent = new {
168-
Name = "Complex Content",
169-
Description = "Detailed information"
165+
Text = "Employee Details: ",
166+
Description = "Alex is a software developer ..."
170167
}
171168
}
172169
};
@@ -178,7 +175,7 @@ public class TabViewViewModel
178175

179176
- Define templates for each data model type in the ResourceDictionary.
180177
- Assign the TabItemTemplateSelector and templates in XAML.
181-
- Bind the CustomSfTabView to the ItemsSource and set the ContentItemTemplateSelector.
178+
- Bind the CustomTabView to the ItemsSource and set the ContentItemTemplateSelector.
182179

183180
```
184181
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
@@ -188,7 +185,7 @@ public class TabViewViewModel
188185
xmlns:tabView="clr-namespace:Syncfusion.Maui.TabView;assembly=Syncfusion.Maui.TabView">
189186
190187
<ContentPage.BindingContext>
191-
<local:TabViewViewModel/>
188+
<local:TabViewModel/>
192189
</ContentPage.BindingContext>
193190
194191
<ContentPage.Resources>
@@ -209,7 +206,7 @@ public class TabViewViewModel
209206
<local:TabItemTemplateSelector.ComplexTemplate>
210207
<DataTemplate>
211208
<StackLayout >
212-
<Label Text="{Binding DetailedContent.Name}" />
209+
<Label Text="{Binding DetailedContent.Text}" />
213210
<Label Text="{Binding DetailedContent.Description}" />
214211
</StackLayout>
215212
</DataTemplate>
@@ -223,16 +220,16 @@ public class TabViewViewModel
223220
</ResourceDictionary>
224221
</ContentPage.Resources>
225222
226-
<local:CustomSfTabView ItemsSource="{Binding TabItems}"
223+
<local:CustomTabView ItemsSource="{Binding TabItems}"
227224
HeaderItemTemplate="{StaticResource headerItemTemplate}"
228225
ContentItemTemplateSelector="{StaticResource TabItemSelector}">
229-
</local:CustomSfTabView>
226+
</local:CustomTabView>
230227
</ContentPage>
231228
```
232229

233230
**Output**
234231

235-
![TabViewItemsSource.gif](https://support.syncfusion.com/kb/agent/attachment/article/18738/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjM0NTE0Iiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5zeW5jZnVzaW9uLmNvbSJ9.QmyNTiwaVaoxcQlQK4vwaXG7FyceJF5ZBuicrry2ghY)
232+
![TabView_Template.gif](https://support.syncfusion.com/kb/agent/attachment/article/18738/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjM1NDQ4Iiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5zeW5jZnVzaW9uLmNvbSJ9.igJJrGUkkq-zMxGuH3swZPNEi2jcvX7Auwh1g_h75_U)
236233

237234
**Requirements to run the demo**
238235

TabViewItemsSource/ViewModel/TabViewViewModel.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

TabViewItemsSource/App.xaml renamed to TabViewSample/App.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version = "1.0" encoding = "UTF-8" ?>
22
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
33
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4-
xmlns:local="clr-namespace:TabViewItemsSource"
5-
x:Class="TabViewItemsSource.App">
4+
xmlns:local="clr-namespace:TabViewSample"
5+
x:Class="TabViewSample.App">
66
<Application.Resources>
77
<ResourceDictionary>
88
<ResourceDictionary.MergedDictionaries>

TabViewItemsSource/App.xaml.cs renamed to TabViewSample/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace TabViewItemsSource
1+
namespace TabViewSample
22
{
33
public partial class App : Application
44
{

TabViewItemsSource/AppShell.xaml renamed to TabViewSample/AppShell.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<Shell
3-
x:Class="TabViewItemsSource.AppShell"
3+
x:Class="TabViewSample.AppShell"
44
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
55
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6-
xmlns:local="clr-namespace:TabViewItemsSource"
6+
xmlns:local="clr-namespace:TabViewSample"
77
Shell.FlyoutBehavior="Flyout"
8-
Title="TabViewItemsSource">
8+
Title="TabViewSample">
99

1010
<ShellContent
1111
Title="Home"

TabViewItemsSource/AppShell.xaml.cs renamed to TabViewSample/AppShell.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace TabViewItemsSource
1+
namespace TabViewSample
22
{
33
public partial class AppShell : Shell
44
{

TabViewItemsSource/Helper/CustomSfTabView.cs renamed to TabViewSample/Helper/CustomTabView.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
using Syncfusion.Maui.TabView;
22

3-
namespace TabViewItemsSource
3+
namespace TabViewSample
44
{
5-
public class CustomSfTabView : SfTabView
5+
public class CustomTabView : SfTabView
66
{
77
// Dependency property for ItemTemplateSelector
88
public static readonly BindableProperty ContentItemTemplateSelectorProperty =
99
BindableProperty.Create(
1010
nameof(ContentItemTemplateSelector),
1111
typeof(DataTemplateSelector),
12-
typeof(CustomSfTabView),
12+
typeof(CustomTabView),
1313
null,
1414
propertyChanged: OnContentItemTemplateSelectorChanged);
1515

@@ -23,15 +23,15 @@ public DataTemplateSelector ContentItemTemplateSelector
2323
// Handle changes to the template selector
2424
private static void OnContentItemTemplateSelectorChanged(BindableObject bindable, object oldValue, object newValue)
2525
{
26-
var tabView = bindable as CustomSfTabView;
26+
var tabView = bindable as CustomTabView;
2727
if (tabView != null)
2828
{
2929
tabView.SetInitialTemplate();
3030
}
3131
}
3232

3333
// Constructor
34-
public CustomSfTabView()
34+
public CustomTabView()
3535
{
3636
// Wire up selection changed event
3737
SelectionChanged += OnSelectionChanged;

TabViewItemsSource/Helper/TabItemTemplateSelector.cs renamed to TabViewSample/Helper/TabItemTemplateSelector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace TabViewItemsSource
1+
namespace TabViewSample
22
{
33
// DataTemplateSelector for TabView
44
public class TabItemTemplateSelector : DataTemplateSelector
@@ -14,7 +14,7 @@ public class TabItemTemplateSelector : DataTemplateSelector
1414
TextTabItem => TextTemplate,
1515
ImageTabItem => ImageTemplate,
1616
ComplexTabItem => ComplexTemplate,
17-
_ => null
17+
_ => TextTemplate
1818
};
1919
}
2020
}
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
33
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4-
x:Class="TabViewItemsSource.MainPage"
5-
xmlns:local="clr-namespace:TabViewItemsSource"
4+
x:Class="TabViewSample.MainPage"
5+
xmlns:local="clr-namespace:TabViewSample"
66
xmlns:tabView="clr-namespace:Syncfusion.Maui.TabView;assembly=Syncfusion.Maui.TabView">
77

88
<ContentPage.BindingContext>
9-
<local:TabViewViewModel/>
9+
<local:TabViewModel/>
1010
</ContentPage.BindingContext>
1111

1212
<ContentPage.Resources>
1313
<ResourceDictionary>
1414
<local:TabItemTemplateSelector x:Key="TabItemSelector">
1515
<local:TabItemTemplateSelector.TextTemplate>
1616
<DataTemplate>
17-
<Label Text="{Binding Content}" VerticalOptions="Center" HorizontalOptions="Center"/>
17+
<Label Text="{Binding Content}" VerticalOptions="Center" HorizontalOptions="Center"
18+
FontAttributes="Bold" FontSize="20" HorizontalTextAlignment="Center"/>
1819
</DataTemplate>
1920
</local:TabItemTemplateSelector.TextTemplate>
2021

2122
<local:TabItemTemplateSelector.ImageTemplate>
2223
<DataTemplate>
23-
<Image Source="{Binding ImageSource}" VerticalOptions="Center" HorizontalOptions="Center"/>
24+
<Image Source="{Binding ImageSource}" WidthRequest="100" HeightRequest="100" VerticalOptions="Center" HorizontalOptions="Center" Aspect="AspectFit"/>
2425
</DataTemplate>
2526
</local:TabItemTemplateSelector.ImageTemplate>
2627

2728
<local:TabItemTemplateSelector.ComplexTemplate>
2829
<DataTemplate>
29-
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
30-
<Label Text="{Binding DetailedContent.Name}" />
31-
<Label Text="{Binding DetailedContent.Description}" />
30+
<StackLayout VerticalOptions="Center" HorizontalOptions="Center" Spacing="10">
31+
<Label Text="{Binding DetailedContent.Text}" FontAttributes="Bold" HorizontalTextAlignment="Center"/>
32+
<Label Text="{Binding DetailedContent.Description}" HorizontalTextAlignment="Start"/>
3233
</StackLayout>
3334
</DataTemplate>
3435
</local:TabItemTemplateSelector.ComplexTemplate>
3536
</local:TabItemTemplateSelector>
3637
<DataTemplate x:Key="headerItemTemplate">
3738
<StackLayout Margin="10">
38-
<Label Text="{Binding Title}" VerticalOptions="Center"/>
39+
<Label Text="{Binding Title}" VerticalTextAlignment="Center"/>
3940
</StackLayout>
4041
</DataTemplate>
4142
</ResourceDictionary>
4243
</ContentPage.Resources>
4344

44-
<local:CustomSfTabView TabBarBackground="LightBlue" IndicatorPlacement="Fill"
45+
<local:CustomTabView TabBarBackground="LightBlue" IndicatorPlacement="Fill"
4546
ItemsSource="{Binding TabItems}"
4647
HeaderItemTemplate="{StaticResource headerItemTemplate}"
4748
ContentItemTemplateSelector="{StaticResource TabItemSelector}">
48-
</local:CustomSfTabView>
49+
</local:CustomTabView>
4950

5051
</ContentPage>

TabViewItemsSource/MainPage.xaml.cs renamed to TabViewSample/MainPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Specialized;
33
using System.Linq;
44

5-
namespace TabViewItemsSource
5+
namespace TabViewSample
66
{
77
public partial class MainPage : ContentPage
88
{

TabViewItemsSource/MauiProgram.cs renamed to TabViewSample/MauiProgram.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Microsoft.Extensions.Logging;
22
using Syncfusion.Maui.Core.Hosting;
33

4-
namespace TabViewItemsSource
4+
namespace TabViewSample
55
{
66
public static class MauiProgram
77
{
@@ -15,6 +15,7 @@ public static MauiApp CreateMauiApp()
1515
{
1616
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
1717
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
18+
fonts.AddFont("MauiMaterialAssets.ttf", "MaterialAssets");
1819
});
1920

2021
#if DEBUG

TabViewItemsSource/Model/BaseTabItem.cs renamed to TabViewSample/Model/BaseTabItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace TabViewItemsSource
1+
namespace TabViewSample
22
{
33

44
public abstract class BaseTabItem

TabViewItemsSource/Model/ComplexTabItem.cs renamed to TabViewSample/Model/ComplexTabItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
namespace TabViewItemsSource
2+
namespace TabViewSample
33
{
44
public class ComplexTabItem : BaseTabItem
55
{

TabViewItemsSource/Model/ImageTabItem.cs renamed to TabViewSample/Model/ImageTabItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace TabViewItemsSource
1+
namespace TabViewSample
22
{
33
public class ImageTabItem : BaseTabItem
44
{

TabViewItemsSource/Model/TextTabItem.cs renamed to TabViewSample/Model/TextTabItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
namespace TabViewItemsSource
2+
namespace TabViewSample
33
{
44
public class TextTabItem:BaseTabItem
55
{

TabViewItemsSource/Platforms/Android/MainActivity.cs renamed to TabViewSample/Platforms/Android/MainActivity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Android.Content.PM;
33
using Android.OS;
44

5-
namespace TabViewItemsSource
5+
namespace TabViewSample
66
{
77
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
88
public class MainActivity : MauiAppCompatActivity

0 commit comments

Comments
 (0)