2013-03-20 3 views
0
<UserControl x:Class="SilverlightApplication4.SilverlightControl1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="400"> 

    <Grid x:Name="LayoutRoot" Background="White" Margin="0,0,80,-20"> 
     <Grid HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Background="Beige"/> 
     <Grid HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Background="Beige" Margin="110,0,0,0"/> 
     <Grid HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Background="Beige" Margin="220,0,0,0"/> 
     <Grid HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Background="Beige" Margin="0,110,0,0"/> 
     <Grid HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Background="Beige" Margin="110,110,0,0"/> 
     <Grid HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Background="Beige" Margin="220,110,0,0"/> 
     <Grid HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Background="Beige" Margin="0,220,0,0"/> 
     <Grid HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Background="Beige" Margin="110,220,0,0"/> 
     <Grid HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Background="Beige" Margin="220,220,0,0"/> 

    </Grid> 
</UserControl> 

위의 코드를 내 xamp 파일에 3 * 3 크기의 격자로 작성했습니다. 9 줄의 코드를 작성하는 대신 코드에서 동일한 작업을 어떻게 수행합니까?코드 뒤에서 격자 그리기

답변

1
private void AddGrids() 
{ 
    for (int i = 0; i < 10; i++) 
    { 
     Grid grid = new Grid 
     { 
      HorizontalAlignment = HorizontalAlignment.Left, 
      VerticalAlignment = VerticalAlignment.Top, 
      Height = 100, 
      Width = 100, 
      Background = new SolidColorBrush(Color.FromArgb(255, 245, 245, 220)), 
      Margin = new Thickness("margin calculated by your algorithm") 
     }; 
     LayoutRoot.Children.Add(grid); 
    } 
}