2016-04-10 6 views
0

こんにちは、私はグリッド内のラベルの内容で動的な行を作成する問題があります。グリッド。C#WPF動的に作成されたグリッド内のラベルは、グリッドの上部に表示されません。

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にラベルを追加することができ、グリッドの上部に通常の表示をします。

<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> 

答えて

0

、私が使用するItemsControlに、私は行を表示したい

MyUserControll 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にコントロールの作成を残すことができます!

+0

ありがとうございます:) –

+0

答えがあなたのために働くならば、それは受け入れられたとマークできますか? – Ireabor

関連する問題