0

I find a solution here to generate form only in code.

But when i try to add a TextBox to the panel, it cannot be edit.

var window = new Window();
var stackPanel = new StackPanel { Orientation = Orientation.Vertical };
stackPanel.Children.Add(new TextBox { Text = "Text" } );
window.Content = stackPanel;
Community
  • 1
  • 1
prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42

1 Answers1

1

I just tested this, and it works fine.

private void Test_Click(object sender, RoutedEventArgs e)
{
    var window = new Window();
    var stackPanel = new StackPanel { Orientation = Orientation.Vertical };
    stackPanel.Children.Add(new TextBox { Text = "Text" });
    window.Content = stackPanel;
    window.Show();
}

<Window x:Class="WPF_Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="Test" Content="Click Me" Click="Test_Click" />
    </Grid>
</Window>

When you click on the button in the Main Window, it opens up a new window with a text box which can be edited.

K Mehta
  • 10,323
  • 4
  • 46
  • 76