I have a TabControl
in my WPF application. I want my application to basically support multiple "instances" within the single program. For example, think about web browsers, they allow you to have multiple instances of websites in different tabs, I want to achieve similar functionality where my application contains several instances of "sub applications".
The problem I currently face is that I have to copy-paste the same XAML to every tab, because each tab has exactly the same markup and UI, but different data. Another problem is that I need functionality to dynamically create those tabs.
Here's a screenshot of my application at its current state. As you can see, there are 2 tabs on the top and the second has transparent background since it's inactive.
So, how do I create a tab-able system where the UI of the tab remains the same for every tab and I only need to develop with one XAML UI and duplicate that for each tab?
Requirements:
Ideally I would love a plain simple sample project/code where there is one unstyled tab control and the application upon startup dynamically creates 2-n tabs which all have the same UI, but with different data.
Add Tabs In order to add new tab item to a tab control, first you have to create instance of the class Telerik. Windows. Controls. RadTabItem, set its properties like Header, Content, ToolTip etc, and then add it to the tab control items collection.
Add a WPF control to a Windows Form Your new WPF control is ready for use on the form. Windows Forms uses the ElementHost control to host WPF content. To add a WPF control to a Windows Form: Open Form1 in the Windows Forms Designer.
User controls, in WPF represented by the UserControl class, is the concept of grouping markup and code into a reusable container, so that the same interface, with the same functionality, can be used in several different places and even across several applications.
As noted in another answer there are probably lots of ways to do this, but here's my simple way:
Define a DataTemplate that defines the content of each your identical tabs. The controls in the data template will bind to the view model of the currently selected tab. I've put a single TextBlock in my example but you can easily extend this.
using this Xaml:
<Page.DataContext>
<Samples:TabBindingViewModels />
</Page.DataContext>
<Grid>
<Grid.Resources>
<DataTemplate x:Key="ContentTemplate"
DataType="{x:Type Samples:TabBindingViewModel}">
<TextBlock Text="{Binding Content}"/>
</DataTemplate>
</Grid.Resources>
<TabControl ContentTemplate="{StaticResource ContentTemplate}"
DisplayMemberPath="Header" ItemsSource="{Binding Items}" />
</Grid>
and this view model code:
public class TabBindingViewModels
{
public TabBindingViewModels()
{
Items = new ObservableCollection<TabBindingViewModel>
{
new TabBindingViewModel(1),
new TabBindingViewModel(2),
new TabBindingViewModel(3),
};
}
public IEnumerable<TabBindingViewModel> Items { get; private set; }
}
public class TabBindingViewModel
{
public TabBindingViewModel() : this(0)
{
}
public TabBindingViewModel(int n)
{
Header = "I'm the header: " + n.ToString(CultureInfo.InvariantCulture);
Content = "I'm the content: " + n.ToString(CultureInfo.InvariantCulture);
}
public string Header { get; set; }
public string Content { get; set; }
}
we get:
I quite like this tutorial on styling the tab control. You can easily put more complex content into the tab headers as well as the content.
You should examine the full template of the tab control to gain an insight into how it works. Use Blend or VS11 beta to extract the template.
In order to dynamically add/delete tabs, now all you need to do is add/delete items to the ObservableCollection of view models.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With