Skip to content

Commit 3ec4833

Browse files
author
SivaranjithN
authored
Update README.md
1 parent 6724cb7 commit 3ec4833

File tree

1 file changed

+105
-2
lines changed

1 file changed

+105
-2
lines changed

README.md

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,105 @@
1-
# How-to-change-the-Tab-view-header-color-using-MVVM-in-.NET-MAUI-TabView
2-
The sample demonstrates how to change the Tab view header color using MVVM in .NET MAUI TabView
1+
This section outlines the step-by-step process to change the Tab view header color using the MVVM pattern within a [.NET MAUI TabView](https://www.syncfusion.com/maui-controls/maui-tab-view). By binding the [TabBarBackground](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.TabView.SfTabView.html#Syncfusion_Maui_TabView_SfTabView_TabBarBackground) property to a color field in the ViewModel, you can dynamically alter the header color.
2+
3+
**Step 1: Initialize TabView in .NET MAUI Page**
4+
5+
Begin by creating a new sample in .NET MAUI. Initialize the TabView control within the content page and set the BindingContext property to associate it with a ViewModel.
6+
7+
**XAML:**
8+
```
9+
<ContentPage.BindingContext>
10+
<local:ViewModel/>
11+
</ContentPage.BindingContext>
12+
13+
```
14+
15+
**Step 2: Create Model and ViewModel Classes**
16+
17+
In the code-behind file, create the necessary Model and ViewModel classes. Establish an Observable collection within the ViewModel to represent the items within the TabView.
18+
19+
**C#**
20+
```
21+
public class Model
22+
{
23+
public string Name { get; set; }
24+
public long Number { get; set; }
25+
}
26+
```
27+
28+
**Step 3: Define Color Field in ViewModel**
29+
30+
Within the ViewModel, create a new color field that will be used to bind to the `TabBarBackground` property of the TabView. Assign the desired color value to this field and expose it through a public property.
31+
32+
**C#**
33+
```
34+
35+
public class ViewModel
36+
{
37+
public Color TabHeaderColor { get; set; }
38+
public ObservableCollection<Model> ContactList { get; set; }
39+
40+
}
41+
public ViewModel()
42+
{
43+
TabHeaderColor = Colors.Aqua;
44+
45+
ContactList = new ObservableCollection<Model>();
46+
ContactList.Add(new Model { Name = "Aaron", Number = 7363750 });
47+
ContactList.Add(new Model { Name = "Adam", Number = 7323250 });
48+
ContactList.Add(new Model { Name = "Adrian", Number = 7239121 });
49+
ContactList.Add(new Model { Name = "Alwin", Number = 2329823 });
50+
ContactList.Add(new Model { Name = "Alex", Number = 8013481 });
51+
ContactList.Add(new Model { Name = "Alexander", Number = 7872329 });
52+
ContactList.Add(new Model { Name = "Barry", Number = 7317750 });
53+
}
54+
55+
}
56+
```
57+
58+
**Step 4: Bind TabBarBackground Property**
59+
60+
In the XAML page, bind the TabBarBackground property of the TabView to the TabHeaderColor property in the ViewModel.
61+
62+
**XAML:**
63+
```
64+
<tabView:SfTabView TabBarBackground="{Binding TabHeaderColor, Mode=TwoWay}" >
65+
<tabView:SfTabItem Header="Calls">
66+
<tabView:SfTabItem.Content>
67+
<Grid BackgroundColor="White" x:Name="AllContactsGrid" >
68+
<ListView x:Name="ContactListView"
69+
ItemsSource="{Binding ContactList}"
70+
RowHeight="75">
71+
<ListView.ItemTemplate>
72+
<DataTemplate>
73+
<ViewCell>
74+
<StackLayout Orientation="Vertical" Margin="30,0,0,0">
75+
<Label Text="{Binding Name}"
76+
FontSize="24"/>
77+
<Label
78+
Text="{Binding Number}"
79+
FontSize="20"
80+
TextColor="LightSlateGray"/>
81+
</StackLayout>
82+
</ViewCell>
83+
</DataTemplate>
84+
</ListView.ItemTemplate>
85+
</ListView>
86+
</Grid>
87+
</tabView:SfTabItem.Content>
88+
</tabView:SfTabItem>
89+
<tabView:SfTabItem Header="Favorites">
90+
<tabView:SfTabItem.Content>
91+
<Grid BackgroundColor="Green" x:Name="FavoritesGrid" />
92+
</tabView:SfTabItem.Content>
93+
</tabView:SfTabItem>
94+
<tabView:SfTabItem Header="Contacts">
95+
<tabView:SfTabItem.Content>
96+
<Grid BackgroundColor="Red" x:Name="AllContacts" />
97+
</tabView:SfTabItem.Content>
98+
</tabView:SfTabItem>
99+
</tabView:SfTabView>
100+
101+
```
102+
103+
**Output:**
104+
105+
![TabColor.png](https://support.bolddesk.com/kb/agent/attachment/article/13621/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEwNTcyIiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5ib2xkZGVzay5jb20ifQ.roY508_YQfK6av2IT8SAqzV5oClccvcHWTooiV-MCLU)

0 commit comments

Comments
 (0)