|
| 1 | +--- |
| 2 | +title: Changing HeaderText in TabView When Tab Item Is Selected |
| 3 | +description: Learn how to dynamically change the HeaderText of TabView items when a tab is selected in UI for .NET MAUI. |
| 4 | +type: how-to |
| 5 | +page_title: Dynamically Update TabView HeaderText Based on Selection in UI for .NET MAUI |
| 6 | +meta_title: Dynamically Update TabView HeaderText Based on Selection |
| 7 | +slug: dynamically-update-tabview-headertext-based-on-selection |
| 8 | +tags: tabview,selectionchanged,event,headertext,ui-for-net-maui |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 11.1.0 | TabView for .NET MAUI | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +I need to change the `HeaderText` of the TabView items when they are selected. For example, when the "Home" tab is selected, its header should change to "Using Home". Similarly, when selecting "Function A", the header should change to "Applying Function A", and so on. |
| 21 | + |
| 22 | +This knowledge base article also answers the following questions: |
| 23 | +- How can I change the header text of TabView dynamically? |
| 24 | +- How to update TabView `HeaderText` based on selection in .NET MAUI? |
| 25 | +- How to use ``SelectionChanged` event in TabView to modify `HeaderText`? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +Use the [SelectionChanged](https://docs.telerik.com/devtools/maui/controls/tabview/selection#events) event of the TabView to dynamically update the header text based on the selected tab item. |
| 30 | + |
| 31 | +1. Subscribe to the `SelectionChanged` event in the TabView. |
| 32 | + |
| 33 | +```xaml |
| 34 | +<telerik:RadTabView x:Name="tabView" |
| 35 | + SelectionChanged="TabView_SelectionChanged"> |
| 36 | + <telerik:TabViewItem HeaderText="Home"/> |
| 37 | + <telerik:TabViewItem HeaderText="Function A"/> |
| 38 | + <telerik:TabViewItem HeaderText="Function B"/> |
| 39 | + <telerik:TabViewItem HeaderText="Function C"/> |
| 40 | +</telerik:RadTabView> |
| 41 | +``` |
| 42 | + |
| 43 | +2. Implement a method to update the `HeaderText` dynamically based on the selected index. Handle the `SelectionChanged` event in the code-behind: |
| 44 | + |
| 45 | +```csharp |
| 46 | +private void TabView_SelectionChanged(object sender, EventArgs e) |
| 47 | +{ |
| 48 | + var headers = new[] { "Home", "Function A", "Function B", "Function C" }; |
| 49 | + for (int i = 0; i < this.tabView.Items.Count; i++) |
| 50 | + { |
| 51 | + if (this.tabView.Items[i] is TabViewItem item) |
| 52 | + { |
| 53 | + item.HeaderText = headers[i]; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + int selectedIndex = this.tabView.SelectedIndex; |
| 58 | + if (this.tabView.Items[selectedIndex] is TabViewItem selectedItem) |
| 59 | + { |
| 60 | + switch (selectedIndex) |
| 61 | + { |
| 62 | + case 0: |
| 63 | + selectedItem.HeaderText = "Using Home"; |
| 64 | + break; |
| 65 | + case 1: |
| 66 | + selectedItem.HeaderText = "Applying Function A"; |
| 67 | + break; |
| 68 | + case 2: |
| 69 | + selectedItem.HeaderText = "Applying Function B"; |
| 70 | + break; |
| 71 | + case 3: |
| 72 | + selectedItem.HeaderText = "Applying Function C"; |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## See Also |
| 80 | + |
| 81 | +- [TabView Overview](https://docs.telerik.com/devtools/maui/controls/tabview/overview) |
| 82 | +- [SelectionChanged Event Documentation](https://docs.telerik.com/devtools/maui/controls/tabview/selection#events) |
0 commit comments