user2261524
user2261524

Reputation: 415

WPF New Window with content

I want create a new Window beside an existing main Windwoe with a scrollable Textbox.

I'm pressing in my main Window on a button "Open New Window" and then it should open a new Window with a scrollable Textbox.

inside form2

In WPF you can drag drop elements in the main Window but cant do that for a new window. So I thought it is only possible when you create a new window in the MainWindow.xaml.cs

I was able to create a new Window trough:

private void btnConnect_Click(object sender, RoutedEventArgs 
 {
    Form form2 = new Form();
    //Do intergreate TextBox with scrollbar in form2

    form2.Show();

 }

and now I want a Textbox

But how can I do that in C# or WPF?

Thx

Upvotes: 4

Views: 31543

Answers (2)

dennis schütz
dennis schütz

Reputation: 383

well... you can create a new Window and load into this Windows.Content a UserControl wich you createt in a new XAML. Example:

NewXamlUserControl ui = new NewXamlUserControl();
MainWindow newWindow = new MainWindow();
newWindow.Content = ui;
newWindow.Show();

the Xaml is could be like this

<UserControl x:Class="Projekt"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       x:Name="newXamlUserControl"      
        Height="300" Width="300">

    <Grid>

        <TextBox Text = ..../>

    </Grid>
</UserControl>

Upvotes: 9

Martin Liversage
Martin Liversage

Reputation: 106956

Create a new WPF window in your project:

  1. Project -> Add New Item -> Window (WPF)
  2. Name the window appropriately (here I use ConnectWindow.xaml)
  3. Add a TextBox to the XAML

    <Window
        x:Class="WpfApplication1.ConnectWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Connect"
        Height="300"
        Width="300"
        ShowInTaskbar="False">
        <TextBox
            AcceptsReturn="True"
            VerticalScrollBarVisibility="Auto"
            HorizontalScrollBarVisibility="Auto"/>
    </Window>
    

    You can customize both Window and TextBox as you like.

There are several ways to display the window.

Displaying a modal window (this refers to the main window):

var window = new ConnectWindow { Owner = this };
window.ShowDialog();
// Execution only continues here after the window is closed.

Displaying a modeless child window:

var window = new ConnectWindow { Owner = this };
window.Show();

Displaying another top-level window:

var window = new ConnectWindow();
window.Show();

Upvotes: 8

Related Questions