|
1 |
| -# How-to-Customize-the-Header-of-the-Selected-Tab-in-.NET-MAUI-TabView-when-using-ItemsSource-binding |
2 |
| -This repository contains a sample explaining how to customize the Header of the Selected Tab in .NET MAUI TabView when using ItemsSource binding. |
| 1 | +This article explains how to customize the selected tab header in the [.NET MAUI TabView](https://www.syncfusion.com/maui-controls/maui-tab-view) when using the [ItemsSource](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.TabView.SfTabView.html#Syncfusion_Maui_TabView_SfTabView_ItemsSource) property. The `VisualStateManager` is designed to work directly with [TabItem](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.TabView.SfTabItem.html#Syncfusion_Maui_TabView_SfTabItem__ctor) but does not integrate seamlessly with the ItemsSource-based tab binding. To achieve the desired customization of the selected tab header, the [SelectionChanged](https://help.syncfusion.com/cr/maui/Syncfusion.Maui.TabView.SfTabView.html?tabs=tabid-1#Syncfusion_Maui_TabView_SfTabView_SelectionChanged) event can be leveraged, by utilizing the [EventToCommandBehavior](https://learn.microsoft.com/en-us/dotnet/communitytoolkit/maui/behaviors/event-to-command-behavior). |
| 2 | + |
| 3 | +To dynamically change the text color of the selected tab header, follow these steps: |
| 4 | + |
| 5 | +**Model** |
| 6 | + |
| 7 | +```csharp |
| 8 | +public class Model: INotifyPropertyChanged |
| 9 | +{ |
| 10 | + public event PropertyChangedEventHandler? PropertyChanged; |
| 11 | + |
| 12 | + protected void OnPropertyChanged(string? propertyName) |
| 13 | + { |
| 14 | + var handler = PropertyChanged; |
| 15 | + if (handler != null) |
| 16 | + handler(this, new PropertyChangedEventArgs(propertyName)); |
| 17 | + } |
| 18 | + |
| 19 | + private string? name; |
| 20 | + public string? Name |
| 21 | + { |
| 22 | + get { return name; } |
| 23 | + set { name = value; OnPropertyChanged("Name"); } |
| 24 | + } |
| 25 | + |
| 26 | + private Color? textColor; |
| 27 | + public Color? TextColor |
| 28 | + { |
| 29 | + get { return textColor; } |
| 30 | + set { textColor = value; OnPropertyChanged("TextColor"); } |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +**ViewModel** |
| 36 | + |
| 37 | +In your ViewModel, you will need to define the properties and commands to handle the selection change: |
| 38 | + |
| 39 | +```csharp |
| 40 | +public class MainPageViewModel: INotifyPropertyChanged |
| 41 | +{ |
| 42 | + public event PropertyChangedEventHandler PropertyChanged; |
| 43 | + |
| 44 | + protected void OnPropertyChanged(string propertyName) |
| 45 | + { |
| 46 | + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| 47 | + } |
| 48 | + |
| 49 | + private ObservableCollection<Model> tabItems; |
| 50 | + public ObservableCollection<Model> TabItems |
| 51 | + { |
| 52 | + get { return tabItems; } |
| 53 | + set { tabItems = value; OnPropertyChanged("TabItems"); } |
| 54 | + } |
| 55 | + |
| 56 | + private Command<object> selectionChangedCommand; |
| 57 | + public Command<object> SelectionChangedCommand |
| 58 | + { |
| 59 | + get { return selectionChangedCommand; } |
| 60 | + set { selectionChangedCommand = value; OnPropertyChanged("SelectionChangedCommand"); } |
| 61 | + } |
| 62 | + |
| 63 | + public MainPageViewModel() |
| 64 | + { |
| 65 | + TabItems = new ObservableCollection<Model> |
| 66 | + { |
| 67 | + new Model() { Name = "Alexandar", TextColor = Colors.Red }, |
| 68 | + new Model() { Name = "Gabriella", TextColor = Colors.Black } |
| 69 | + // Add more items as needed |
| 70 | + }; |
| 71 | + |
| 72 | + SelectionChangedCommand = new Command<object>(ExecuteSelectionChangedCommand); |
| 73 | + } |
| 74 | + |
| 75 | + public void ExecuteSelectionChangedCommand(object obj) |
| 76 | + { |
| 77 | + if (obj is SfTabView tabItem) |
| 78 | + { |
| 79 | + if (TabItems != null) |
| 80 | + { |
| 81 | + // Ensure the selected index is within the valid range |
| 82 | + if (tabItem.SelectedIndex >= 0 && tabItem.SelectedIndex < TabItems.Count) |
| 83 | + { |
| 84 | + for (int i = 0; i < TabItems.Count; i++) |
| 85 | + { |
| 86 | + // Set TextColor for selected tab to red, others to black |
| 87 | + TabItems[i].TextColor = (i == (int)tabItem.SelectedIndex) ? Colors.Red : Colors.Black; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +**Explanation** |
| 97 | + |
| 98 | +1. **TextColor Property**: A `TextColor` property is defined within the `Model` class to allow dynamic updates to the text color of the tab headers. |
| 99 | +2. **SelectionChangedCommand**: This command is bound to the `SelectionChanged` event of the `.NET MAUI TabView`. When the selection changes, the command is executed with the `.NET MAUI TabView` instance as the command parameter. |
| 100 | +3. **ExecuteSelectionChangedCommand Method**: This method resets the text color for all tab items to the default color (black) and updates the text color of the currently selected tab to red. |
| 101 | + |
| 102 | +**XAML** |
| 103 | + |
| 104 | +```xml |
| 105 | +<tabView:SfTabView x:Name="tabView" ItemsSource="{Binding TabItems}" TabWidthMode="SizeToContent" TabHeaderPadding="0"> |
| 106 | + <tabView:SfTabView.Behaviors> |
| 107 | + <toolkit:EventToCommandBehavior Command="{Binding SelectionChangedCommand}" CommandParameter="{x:Reference tabView}" EventName="SelectionChanged"/> |
| 108 | + </tabView:SfTabView.Behaviors> |
| 109 | + <tabView:SfTabView.HeaderItemTemplate> |
| 110 | + <DataTemplate > |
| 111 | + <Label x:Name="headerlabel" VerticalOptions="Center" Text="{Binding Name}" |
| 112 | + TextColor="{Binding TextColor}"/> |
| 113 | + </DataTemplate> |
| 114 | + </tabView:SfTabView.HeaderItemTemplate> |
| 115 | + <tabView:SfTabView.ContentItemTemplate> |
| 116 | + <DataTemplate> |
| 117 | + <Grid HorizontalOptions="Center" VerticalOptions="Center"> |
| 118 | + <Label x:Name="contentLabel" Text="{Binding Name}" HorizontalOptions="Center" VerticalOptions="Center"/> |
| 119 | + </Grid> |
| 120 | + </DataTemplate> |
| 121 | + </tabView:SfTabView.ContentItemTemplate> |
| 122 | +</tabView:SfTabView> |
| 123 | +``` |
| 124 | + |
| 125 | +By following these steps, you can effectively customize the selected tab header in a .NET MAUI TabView when using ItemsSource binding. |
| 126 | + |
| 127 | +**Output** |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +**Requirements to run the demo** |
| 132 | + |
| 133 | +To run the demo, refer to [System Requirements for .NET MAUI](https://help.syncfusion.com/maui/system-requirements) |
| 134 | + |
| 135 | +**Troubleshooting:** |
| 136 | + |
| 137 | +**Path too long exception** |
| 138 | + |
| 139 | +If you are facing path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project. |
0 commit comments