0

I have a window in which I have a TabControl. Whenever I press a certain button there should be a tabitem added with layout below.

Do I have to convert my XAML code to C# manual or is there any other option? Also after I have created this tabitem I need to able to edit it which means I need some reference to each tabitem and be able indentify which is which.

<TabItem Header="Table 1">
                <DockPanel>
                    <Grid DockPanel.Dock="Top" Margin="5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="2*"/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>

                        <DataGrid Grid.Column="0">
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Order number"/>
                                <DataGridTextColumn Header="Customer"/>
                                <DataGridTextColumn Header="Total Price"/>
                                <DataGridComboBoxColumn Header="Urgency"/>
                            </DataGrid.Columns>

                        </DataGrid>

                        <StackPanel Grid.Column="1">
                            <Label Content="Maximum Seats:"/>
                            <Label Content="Current connections:"/>
                        </StackPanel>

                        <DockPanel Grid.Column="2" LastChildFill="False" HorizontalAlignment="Right">
                            <Button Name="BtnEditTable" Content="Edit Table" DockPanel.Dock="Top" Click="BtnEditTable_Click"/>
                        </DockPanel>
                    </Grid>
                    <TabControl Margin="5,0,0,0">
                        <TabItem Header="Customer 1">
                            <StackPanel>
                                <Label Content="Connected:"/>
                                <DataGrid>
                                    <DataGrid.Columns>
                                        <DataGridTextColumn Header="Order number"/>
                                        <DataGridTextColumn Header="Name"/>
                                        <DataGridTextColumn Header="Price"/>
                                    </DataGrid.Columns>
                                </DataGrid>
                            </StackPanel>
                        </TabItem>
                    </TabControl>
                </DockPanel>
            </TabItem>
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
asd
  • 45
  • 4
  • You should bind the TabControl's ItemsSource to a collection of items that hold the data of each tab. – Clemens Jan 08 '19 at 13:04

1 Answers1

0

You can use Name property in XAML and then access the object in code-behind. For example

<Grid Name="myGrid"> ... </Grid>

Example code behind:

myGrid.Visibility = Visibility.Collapsed;
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • But I want to create as much tabitems as I want via a button and that would be feasible with this solution as I would have to create all the tabitems in advance – asd Jan 08 '19 at 12:56
  • @asd Why not use `TabItem tabItem = new TabItem();` ? – Michał Turczyn Jan 08 '19 at 12:59
  • Because I want it to have the layout from above and that would mean many lines of code – asd Jan 08 '19 at 13:02
  • If I understand you correctly, there is no other way. You can create objects only in code-behind. – Michał Turczyn Jan 08 '19 at 13:03
  • "*You can create objects only in code-behind*" is blatantly wrong. With WPF, dynamically created view elements are typically created by DataTemplates, declared in XAML. – Clemens Jan 08 '19 at 13:15
  • @Clemens Yes, of course, but I mean creating object on your own, using constructor :) You don't create objects explicitly in XAML, they are created implicitly. – Michał Turczyn Jan 08 '19 at 13:29