Visual C# 2013 - Element disappearing when adding a new one in WPF form editor

时间:2022-02-18 14:46:35

When I try adding more than one element to my WPF form in the editor in VC#2013, the previous element disappears. In the end, I can't have more than one item in the form. I've already written some code so I'd prefer not starting again from scratch. The form has nothing special besides being borderless, fullscreen and starting maximized. This is the XAML code for the form right now:

当我尝试在VC#2013的编辑器中向我的WPF表单添加多个元素时,前一个元素消失了。最后,我不能在表单中有多个项目。我已经编写了一些代码,所以我不想从头开始。除了无边框,全屏和最大化之外,表单没有什么特别之处。这是目前表单的XAML代码:

    <Window x:Class="queue_bigscreen.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="1080" Width="1920" WindowStyle="None" ResizeMode="NoResize" WindowState="Maximized" Background="#FF9EA7CD">
    <Label x:Name="nowServingLabel" Content="0" Margin="42,56,1160,131" Foreground="White" Height="893" FontSize="700" HorizontalContentAlignment="Center">
        <Label.Effect>
            <DropShadowEffect ShadowDepth="13"/>
        </Label.Effect>
    </Label>
</Window>

And this is what I get after I select a textbox and try adding it to the form:

这是我选择文本框并尝试将其添加到表单后得到的结果:

<Window x:Class="queue_bigscreen.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="1080" Width="1920" WindowStyle="None" ResizeMode="NoResize" WindowState="Maximized" Background="#FF9EA7CD">
<TextBlock HorizontalAlignment="Left" Margin="1332,382,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>

As you can see, the label disappears, and the textbox I added in turn disappears if I try adding something else. Am I doing something wrong or is it a known bug?

正如您所看到的,标签消失了,如果我尝试添加其他内容,我添加的文本框也会消失。我做错了什么或者它是一个已知的错误?

1 个解决方案

#1


5  

You need to put the items in a container. Window can only have a single root element, so to get multiple elements on the form, you need to have an element that allows children.

您需要将项目放在容器中。 Window只能有一个根元素,因此要在表单上获取多个元素,您需要有一个允许子元素的元素。

The closest to Windows Forms Form would be Grid. You can then put controls in that, with absolute and relative positioning. It's also the default, so I assume you accidentally deleted it from your XAML (or by being too aggressive with pressing delete in the designer).

最接近Windows窗体表单的将是Grid。然后,您可以使用绝对和相对定位来放置控件。它也是默认值,因此我假设您不小心将其从XAML中删除(或者在设计器中按删除过于激进)。

Example form with a label and a textbox:

带有标签和文本框的示例表单:

<Window x:Class="WpfApplication1.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>
        <Label Content="Label" HorizontalAlignment="Left" Margin="23,30,0,0" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="66,32,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Window>

#1


5  

You need to put the items in a container. Window can only have a single root element, so to get multiple elements on the form, you need to have an element that allows children.

您需要将项目放在容器中。 Window只能有一个根元素,因此要在表单上获取多个元素,您需要有一个允许子元素的元素。

The closest to Windows Forms Form would be Grid. You can then put controls in that, with absolute and relative positioning. It's also the default, so I assume you accidentally deleted it from your XAML (or by being too aggressive with pressing delete in the designer).

最接近Windows窗体表单的将是Grid。然后,您可以使用绝对和相对定位来放置控件。它也是默认值,因此我假设您不小心将其从XAML中删除(或者在设计器中按删除过于激进)。

Example form with a label and a textbox:

带有标签和文本框的示例表单:

<Window x:Class="WpfApplication1.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>
        <Label Content="Label" HorizontalAlignment="Left" Margin="23,30,0,0" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="66,32,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Window>