How in TabControl correctly organize the work of tabs and elements inside tabs? For example, 1 tab ( TabItem1 ) works with an RS232 port and has its own settings, controls and properties. Tab 2 ( TabItem2 ) works with a USB port and has its own settings, controls and properties. When adding a tab, choose from the list with which port to work and, accordingly, the tab should be added and in this tab should be those settings , controls and properties that belong to the selected port. Here is a link to the picture https://www.dropbox.com/s/2widu5i5uvqtrxv/TabControl.jpg

  • one
    @MAKSIM, maybe this will help: habrahabr.ru/post/111105 Create for each tab its own view model, which you associate with the corresponding view (tab). - Pleshkov Ivan
  • @MAKSIM: Read about MVVM. No bikes are needed, all logic is reduced to the timely addition of an element to the ViewModels collection, which are responsible for tabs. (The ViewModel is the view model mentioned in the comment by @Pleshkov Ivan). - VladD

1 answer 1

I advise you to use the MVVM approach. To do this, you will need to create tab view models:

 public abstract class TabVm : NotificationObject { public string Header { get; } public TabVm(string header) { Header = header; } } public class Tab1Vm : TabVm { public Tab1Vm() : base("Tab 1") { } } public class Tab2Vm : TabVm { public Tab2Vm() : base("Tab 2") { } } 

NotificationObject is a class that implements INotifyPropertyChanged . TabVm is the base view model for all tabs. Tab1Vm and Tab2Vm - specific tabs.

Next, we define the view model that will manage the tabs:

 public class MainVm : NotificationObject { public ObservableCollection<TabVm> Tabs { get { return _tabs ?? (_tabs = new ObservableCollection<TabVm>()); } } private ObservableCollection<TabVm> _tabs; public TabVm SelectedTab { get { return _selectedTab; } set { SetProperty(ref _selectedTab, value); } } private TabVm _selectedTab; public MainVm() { Tabs.Add(new Tab1Vm()); Tabs.Add(new Tab2Vm()); SelectedTab = Tabs.FirstOrDefault(); } } 

This implementation does not control anything, but simply forms a list of tabs. Dale, you can wind up any logic.

And finally, we define the presentation:

 <Window ...> <Window.DataContext> <vm:MainVm/> </Window.DataContext> <Window.Resources> <ResourceDictionary> <DataTemplate DataType="{x:Type vm:Tab1Vm}"> <TextBlock Text="Tab 1 Content"/> </DataTemplate> <DataTemplate DataType="{x:Type vm:Tab2Vm}"> <TextBlock Text="Tab 2 Content"/> </DataTemplate> </ResourceDictionary> </Window.Resources> <Grid> <TabControl ItemsSource="{Binding Tabs}" SelectedItem="{Binding SelectedTab}"> <TabControl.Resources> <Style TargetType="TabItem"> <Setter Property="HeaderTemplate"> <Setter.Value> <DataTemplate DataType="{x:Type TabItem}"> <TextBlock Text="{Binding Header}"/> </DataTemplate> </Setter.Value> </Setter> </Style> </TabControl.Resources> </TabControl> </Grid> </Window> 

I defined tab templates right inside the window. You can put them into separate files, or define your DataTemplateSelector and use it.

You can also use Ribbon instead of TabControl.