Universal Windows Platform (UWP)
A Microsoft platform for building and publishing apps for Windows desktop devices.
2,934 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I'm trying to add content to a TabViewItem dynamically but don't know which option to use to do this when calling the TabViewItem itself. The code I tried isn't working for me for some reason.
public MainPage()
{
this.InitializeComponent();
TabView tabView = MyTabs;
var tabFlower = new TabViewItem { "Flower" };
tabView.Items.Add(tabFlower );
// Tab content
var txtDetails= new TextBlock();
txtDetails.Text = "Lorem Ipsum";
tabFlower.Add(txtDetails);
}
Hi, try following demo with Microsoft.Toolkit.Uwp.UI.Controls.TabView:
<Page
x:Class="App1.Page05"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App05"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.DataContext>
<local:ViewModel/>
</Page.DataContext>
<Grid>
<controls:TabView ItemsSource="{Binding View}"/>
</Grid>
</Page>
----------------------------------------------------------
using Microsoft.Toolkit.Uwp.UI.Controls;
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Windows.UI.Xaml.Controls;
namespace App05
{
public class ViewModel
{
public ViewModel() => InsertNewTabItem();
public ObservableCollection<TabViewItem> View { get; } = new ObservableCollection<TabViewItem>();
private int index = 1;
private void InsertNewTabItem()
{
var ti = new TabViewItem() { Header = $"Tab {index++}" };
View.Add(ti);
// Tab content Panel
StackPanel stp = new StackPanel() { Height = 100 };
ti.Content = stp;
// panel text content
var txtDetails = new TextBlock();
txtDetails.Text = $"This is text in Tabitem '{ti.Header}'";
stp.Children.Add(txtDetails);
// panel button content
var btn = new Button() { Content = "Add new Tab" };
btn.Command = new RelayCommand((state) => InsertNewTabItem());
stp.Children.Add(btn);
}
}
public class RelayCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
public event EventHandler CanExecuteChanged;
public RelayCommand(Action<object> execute) : this(execute, null) { }
public RelayCommand(Action<object> execute, Predicate<object> canExecute) { _execute = execute; _canExecute = canExecute; }
public bool CanExecute(object parameter) => (_canExecute == null) ? true : _canExecute(parameter);
public void Execute(object parameter) => _execute(parameter);
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}