C# WPF Dynamic 在网格中创建的标签不显示在网格顶部

C# WPF Dynamic created Labels in a Grid dont show on the Top of the Grid

你好(抱歉英语不好)我有一个问题,我在网格中创建了带有标签内容的动态行,但它们不是从网格的顶部开始的。

private void SelectAccounts(string AccountUsername) //Findet Nutzer und Account ID und gibt alle Accounts des Nutzers aus //// neu machen jede reihe einzeln zurückgeben!!!!!!!
    {
        MId=database.GetID(this.MasterUsername,"MasterUser","MId"); //for database request 
        AccId = database.GetID(AccountUsername, "Accounts","AccId"); //for database request 
        AccountsList.Children.Clear(); //My Grid where i safe it

        int rows = database.GetRows("Accounts", "AccID"); //How many Rows i have in my table 
        for (int i = 1; i <= rows; i++)
        {
            stringusername=database.GetUsernameById("Accounts","Username", i, MId); //getting the username back as string 


            if(username!="") // not evry username has the right condition so somtimes the function returns me only ""
            {
                Label l = new Label();
                RowDefinition newRow = new RowDefinition();
                newRow.Height = new GridLength(40);

                AccountsList.RowDefinitions.Add(newRow);
                l.Content = username;

                AccountsList.Children.Add(l);
                l.SetValue(Grid.RowProperty, i);
            }
        } 
    }

问题是当我现在启动程序时它看起来像这样: enter image description here

如果我像这样使用循环:

for (int i = 1; i <= rows; i++)
{
    Label l = new Label();
    RowDefinition newRow = new RowDefinition();
    newRow.Height = new GridLength(40);
    AccountsList.RowDefinitions.Add(newRow);
    string username= database.GetUsernameById("Accounts", "Username", i, MId);

    if(username!="")
    {
        l.Content = username;

        AccountsList.Children.Add(l);
        l.SetValue(Grid.RowProperty, i);
    }
}

看起来像这样:enter image description here

我可以正常在.xaml中添加标签,它在网格顶部显示正常。

MyUserControll 我想在其中显示行:

<UserControl x:Class="AccountManager.Login"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="500" d:DesignWidth="750">
<Grid Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="100"/>
        <RowDefinition Height="400"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="13*"/>
        <ColumnDefinition Width="37*"/>
    </Grid.ColumnDefinitions>
    <Grid Grid.Row="0" Grid.Column="0">
        <Label Content="Your Accounts" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="21.333" FontFamily="Tekton Pro" FontWeight="Bold"></Label>
        <Border BorderBrush="LightBlue" BorderThickness="1.5" HorizontalAlignment="Right"/>
        <Border BorderBrush="LightBlue" BorderThickness="1.5" VerticalAlignment="Bottom"/>
        <Border BorderBrush="Blue" BorderThickness="1.5" VerticalAlignment="Bottom" Opacity="0.2"/>
        <Border BorderBrush="Blue" BorderThickness="1.5" HorizontalAlignment="Right" Opacity="0.2"/>
    </Grid>
    <Grid Grid.Row="1" Grid.Column="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="146*"/>
            <ColumnDefinition Width="348*"/>
            <ColumnDefinition Width="91*"/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Row="0" Grid.Column="0">
            <Border BorderBrush="LightBlue" BorderThickness="1.5" HorizontalAlignment="Right"/>
            <Border BorderBrush="Blue" BorderThickness="1.5" Opacity="0.2" HorizontalAlignment="Right"/>
        </Grid>
        <Grid Grid.Column="1">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="110*"/>
                <ColumnDefinition Width="191*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Label  Content="Benutzername:" VerticalAlignment="Center" HorizontalAlignment="Right" Height="26" Width="90"/>
            <TextBox x:Name="addBenutzername" Grid.Row="0" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Right" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1" />
            <PasswordBox x:Name="addPw" Grid.Row="1" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Right" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1" ></PasswordBox>
            <Label Grid.Row="1" Grid.Column="0" Content="Password:" VerticalAlignment="Center" HorizontalAlignment="Right" Height="26" Width="62"/>
            <Label x:Name="repeatPasswordLabel" Grid.Row="2" Grid.Column="0" Content="Repeat Password:" HorizontalAlignment="Right" VerticalAlignment="Center" Height="26" Width="102" />
            <PasswordBox x:Name="addReapeatPw" Grid.Row="2" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Right" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1" />
            <Label Content="Email:" Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" Height="26" Width="41"/>
            <TextBox x:Name="addEmail" Grid.Row="3" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Right" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1"/>
            <Button Grid.Row=" 5" Grid.Column="1" Content="ADD" Height="36" HorizontalAlignment="Center" VerticalAlignment="Center" Width="80" Click="AddAccount"></Button>
        </Grid>
        <Grid Grid.Column="2">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <RadioButton x:Name="r1" Grid.Row="0" Width="70" Height="30">
                <Image Source="Bilder/Facebook.png"></Image>

            </RadioButton>
            <RadioButton x:Name="r2" Grid.Row="1" Width="70" Height="30">
                <Image Source="Bilder/Skype.png"></Image>
            </RadioButton>
            <RadioButton x:Name="r3" Grid.Row="2" Width="70" Height="30">
                <Image Source="Bilder/Youtube.png"></Image>
            </RadioButton>
            <RadioButton x:Name="r4" Grid.Row="3" Width="70" Height="30">
                <Image Source="Bilder/Twitter.png"></Image>
            </RadioButton>
            <RadioButton x:Name="r5" Grid.Row="4" Width="70" Height="30">
                <Image Source="Bilder/Microsoft.png"></Image>
            </RadioButton>
            <RadioButton x:Name="r6" Grid.Row="5" Width="70" Height="30">
                <Image Source="Bilder/reddit.png"></Image>
            </RadioButton>
            <RadioButton x:Name="r7" Grid.Row="6" Width="70" Height="30">
                <Image Source="Bilder/Origin.png"></Image>
            </RadioButton>
            <RadioButton x:Name="r8" Grid.Row="8" Width="70" Height="30">
                <Image Source="Bilder/Steam.png"></Image>
            </RadioButton>
        </Grid>
    </Grid>
    <Grid VerticalAlignment="Top" x:Name="AccountsList" Grid.Row="1" Grid.Column="0">
        <Border BorderBrush="LightBlue" BorderThickness="1.5" HorizontalAlignment="Right"/>
        <Border BorderBrush="Blue" BorderThickness="1.5" HorizontalAlignment="Right"  Opacity="0.2"/>



    </Grid>
    <Grid Grid.Row="0" Grid.Column="1">
        <Border BorderBrush="LightBlue" BorderThickness="1.5" VerticalAlignment="Bottom"/>
        <Border BorderBrush="Blue" BorderThickness="1.5" VerticalAlignment="Bottom" Opacity="0.2"/>
        <Button x:Name="delete" Content="Delete" HorizontalAlignment="Left" Width="80" Height="36" Margin="85,19,0,19"/>
        <Button x:Name="change" Content="Change" HorizontalAlignment="Center" VerticalAlignment="Center" Width="80" Height="36"></Button>




    </Grid>
</Grid>

我的主要 Windows 是:

<Window x:Class="AccountManager.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="500" Width="750" MaxWidth="750" MinWidth="750" MinHeight="500" MaxHeight="500">
<Grid x:Name="Main">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="371"></ColumnDefinition>
            <ColumnDefinition Width="371"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="80"></RowDefinition>
            <RowDefinition Height="190"></RowDefinition>

        </Grid.RowDefinitions>
        <Label Grid.Row="0" Content="Accountmanager" HorizontalAlignment="Center"  VerticalAlignment="Center"  Background="White" FontSize="24" FontFamily="Snap ITC"/>
        <Grid Grid.Row="0" Grid.Column="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150"></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>

        </Grid>
        <Grid Grid.Row="1" x:Name="bilder">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <Image Grid.Row="0" Source="Bilder/Facebook.png" Height="70" HorizontalAlignment="Center" VerticalAlignment="Center" Width="70"/>
            <Image Grid.Row="0" Grid.Column="1" Source="Bilder/Skype.png" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            <Image Grid.Row="0" Grid.Column="2" Source="Bilder/Youtube.png" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            <Image Grid.Row="0" Grid.Column="3" Source="Bilder/Twitter.png" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <Image Grid.Row="1" Grid.Column="0" Source="Bilder/Microsoft.png" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <Image Grid.Row="1" Grid.Column="1" Source="Bilder/reddit.png" HorizontalAlignment="Center" VerticalAlignment="Center"></Image>
            <Image Grid.Row="1" Grid.Column="2" Source="Bilder/Origin.png" VerticalAlignment="Center" HorizontalAlignment="Center"></Image>
            <Image Grid.Row="1" Grid.Column="3" Source="Bilder/Steam.png" VerticalAlignment="Center" HorizontalAlignment="Center"></Image>

        </Grid>
        <Grid Grid.Row="1" Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150"></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Label  Content="Benutzername:" VerticalAlignment="Top" HorizontalAlignment="Right"/>
            <TextBox x:Name="benutzername" Grid.Row="0" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Black" BorderThickness="1"></TextBox>
            <PasswordBox x:Name="pw" Grid.Row="0" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Left" BorderBrush="Black" BorderThickness="1" VerticalAlignment="Bottom"/>
            <Label  Grid.Row="0" Grid.Column="0" Content="Password:" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
            <Button Grid.Column="0" Grid.Row="1" Content="Add" Width="80" Height="40" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="47,0,23,0" Click="ClickAddAccount"/>
            <Button Grid.Column="1" Grid.Row="1" Content="Login" Width="80" Height="40" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,23,0" Click="Login" />
            <Button Grid.Column="1" Grid.Row="1" Content="Forgot Password" HorizontalAlignment="Left" VerticalAlignment="Bottom" Height="40" Width="95" Click="ForgotPw" />

        </Grid>





    </Grid>
</Grid>

在您的位置,我将使用 ItemsControl 并创建一个对象以将 ItemsSource 绑定到。像这样创建一个 ItemsControl:

<ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Window}, 
Path=Header}">//Instead of header, use your own sourceobject
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            //Insert your Itemtemplate here, you can bind to any object in the ItemsSource!
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

这可以避免您手动创建每个控件的麻烦,您可以将控件的创建留给 wpf!