JSchwartz
JSchwartz

Reputation: 2724

How to programatically add a datagrid to a stackpanel in WPF?

I have a stackpanel in my XAML called MainStackPanel. Inside my XAML.CS I am programatically creating a few datagrids like this:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var datagrid = new DataGrid();
        // add some columns and stuff
    }

This is just an example, in reality I am creating a dozen or so (depending on the results from the database).

I need to add them to my stackpanel (defined in my XAML file as follows:

    <ScrollViewer Name="MainScrollViewer" Grid.Row="0">
        <StackPanel Name="MainStackPanel">
        // in here somehow
        </StackPanel>
    </ScrollViewer>

How can I programatically add my datagrids (in this case var datagrid) to the stackpanel (MainStackPanel)? Thanks,

Upvotes: 0

Views: 1053

Answers (1)

user1064519
user1064519

Reputation: 2190

MainStackPanel.Children.Add(datagrid);

Upvotes: 2

Related Questions