4

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

4

2 回答 2

9

好吧...您可以创建一个新窗口并将其加载到此 Windows.Content 中,您可以在新的 XAML 中创建一个 UserControl。例子:

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

Xaml 可能是这样的

<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>
于 2013-04-25T11:29:21.033 回答
7

在您的项目中创建一个新的 WPF 窗口:

  1. 项目 -> 添加新项目 -> 窗口 (WPF)
  2. 适当地命名窗口(这里我使用ConnectWindow.xaml
  3. 添加TextBox到 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>
    

    Window您可以根据需要自定义两者TextBox

有几种方法可以显示窗口。

显示模态窗口(this指主窗口):

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

显示无模式子窗口:

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

显示另一个顶级窗口:

var window = new ConnectWindow();
window.Show();
于 2013-04-25T12:09:46.540 回答