Hi,
you can use ItemsControl to display a variable count of UI elements. try following demo.
XAML:
<Window x:Class="Window93"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="Window93" Height="450" Width="800">
<Window.DataContext>
<local:Window93VM/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<ComboBox ItemsSource="{Binding SelectList}"
SelectedItem="{Binding SelectedVersion}"
Margin="5"/>
<ScrollViewer Grid.Row="1">
<ItemsControl ItemsSource="{Binding Elements}"/>
</ScrollViewer>
</Grid>
</Window>
And ViewModel:
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Public Class Window93VM
#Region " prepare demo"
Public Sub New()
' connect collection to source for displaying elements
cvs.Source = col
' prepare data for demo
For i = 1 To _data.GetUpperBound(0)
Data(i) = $"Data {i}"
Next
' prepare control list for demo
Dim rnd As New Random
For i = 1 To 10
Dim l As New List(Of Integer)
For k = 1 To rnd.Next(1, 20)
l.Add(rnd.Next(1, 101))
Next
controlList.Add($"Version {i}", l)
Next
End Sub
#End Region
#Region " collection of framework elements for display (in ItemsSource)"
Private cvs As New CollectionViewSource
Private col As New ObservableCollection(Of FrameworkElement)
Public ReadOnly Property Elements As ICollectionView
Get
Return cvs.View
End Get
End Property
#End Region
#Region " list for select elements to display (in ComboBox)"
Private controlList As New Dictionary(Of String, List(Of Integer))
Public ReadOnly Property SelectList As List(Of String)
Get
Return controlList.Keys.ToList
End Get
End Property
Private _selectedVersion As String
Public Property SelectedVersion As String ' selected item in SelectList
Get
Return Me._selectedVersion
End Get
Set(value As String)
Me._selectedVersion = value
LoadData(controlList(value))
End Set
End Property
#End Region
#Region " array of data for binding"
Private _data(100) As String
Public Property Data(index As Integer) As String
Get
Return Me._data(index)
End Get
Set(value As String)
Me._data(index) = value
End Set
End Property
#End Region
#Region " load elements to display"
Private Sub LoadData(cList As List(Of Integer))
col.Clear() ' clear list for display in ItemsControl
For Each item In cList
Dim tb As New TextBox ' element to display (TextBox or other UserControl)
Dim b As New Binding($"Data[{item}]") ' prepare binding to data
tb.SetBinding(TextBox.TextProperty, b) ' set binding
tb.Margin = New Thickness(5) ' set additional properties
col.Add(tb) ' add element to display in ItemsControl
Next
End Sub
#End Region
End Class