Mateus Rocha Ramos
Mateus Rocha Ramos

Reputation: 21

WPF ListView - Add Dynamic

I have a TabControl in XAML code:

<TabItem Name="tabItem1" Header="Tab1">
  <ListBox Name="lstValues"
           Background="{x:Null}"
           BorderBrush="{x:Null}">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <Border BorderThickness="1"
                Margin="0,2,0,0"
                BorderBrush="SteelBlue"
                CornerRadius="4">
          <Grid>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="30" />
              <ColumnDefinition Width="25" />
              <ColumnDefinition Width="75" />
            </Grid.ColumnDefinitions>
            <Button Name="btnRemoveValue"
                    Grid.Column="0"
                    Height="25"
                    Margin="5,2,0,2"
                    Background="DarkRed”/>
            <TextBlock Grid.Column="1"
                       Text="Step"/>   
            <TextBox Grid.Column="2"
                     Margin="35,1,1,1"
                     Height="32"/>
          </Grid>
        </Border>
      </DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>
</TabItem>

When the user clicks in determinated button, I need to add a new ListBox with this template and Items. How many times the user click?

Upvotes: 1

Views: 228

Answers (1)

quicoli
quicoli

Reputation: 622

you should bind your tabcontrol.ItemsSource to an object list and set its ItemTemplate and ContentTemplate. So, when user clicks, you add a new object the object list. Something like this:

<TabControl Margin="0,5,0,0" 
            IsTabStop="False"
            Focusable="False"
            ItemsSource="{Binding MyObjectCollection, 
                UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
  <TabControl.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal"
                  IsEnabled="False"
                  Focusable="False"
                  HorizontalAlignment="Center">
        <TextBlock Text="Versão:" Focusable="False"/>
        <TextBlock Text="{Binding Sequencia}" Focusable="False"/>
      </StackPanel>
    </DataTemplate>
  </TabControl.ItemTemplate>
  <TabControl.ContentTemplate>
    <DataTemplate>
      <ListBox Background="{x:Null}" 
               BorderBrush="{x:Null}" ItemsSource="{Binding ItemCollection}" >
        <ListBox.ItemTemplate>
          <DataTemplate>
            <Border BorderThickness="1"
                    Margin="0,2,0,0"
                    BorderBrush="SteelBlue"
                    CornerRadius="4">
              <Grid>
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="30"/>
                  <ColumnDefinition Width="25"/>
                  <ColumnDefinition Width="75"/>
                </Grid.ColumnDefinitions>

                <Button Name="btnRemoveValue"
                        Grid.Column="0"
                        Height="25"
                        Margin="5,2,0,2"
                        Background="DarkRed"/>
                <TextBlock Grid.Column="1"
                           Text="Step"/>
                <TextBox Grid.Column="2"
                         Margin="35,1,1,1"
                         Height="32"/>
              </Grid>
            </Border>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>
    </DataTemplate>
  </TabControl.ContentTemplate>
</TabControl>

MyObjectCollection is a collection os objects you wanna present in the view.

Upvotes: 2

Related Questions