I'm trying to dynamically add items to the StackPanel.
<StackPanel Name="Panel1" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" Height="auto" Margin="0,41,0,0" VerticalAlignment="Top" Width="auto"> </StackPanel> Here is the class where I'm trying to add:
public class DynamicCreator { MainWindow main = new MainWindow(); public void TextBoxCreator(int n) { for (int i = 0; i < n; i++) { main.Panel1.Children.Add(new Label { Content = "Test" }); } main.UpdateLayout(); } } But the elements do not appear. What to do? Where is the mistake?
Labelto theStackPanelat the touch of a button. For this, I made a separate class, where there is aTextBoxCreatormethod, which should use the loop to add these elements. If I do the same fromMainWindow.xaml.cs, then everything works, but I want to understand how to add elements to the MainWindow from another class. - NikitaObservableCollection, a collection that reports its change. - Ev_Hyper