http://msdn.microsoft.com/zh-cn/library/system.windows.controls.grid(v=vs.95).aspx
Grid控件:
支持网格布局的控件,可定义网格的行数和列数,可根据屏幕尺寸自动缩放。
Grid.Row : 表示第几行,数值从0开始。获取或设置一个值,该值指示 Grid 中的子内容应出 现在哪个行内。
Grid.Column: 表示第几列,数值从0开始。获取或设置一个值,该值指示 Grid 中的子内容应出 现在哪列中。
Grid.RowSpan:获取或设置一个值,该值指示 Grid 中的子内容所跨越的总行数。
Grid.ColumnSpan:获取或设置一个值,该值指示 Grid 中的子内容所跨越的总列数。
完全按照附加属性Grid.Row,Grid.Column,Grid.RowSpan,Grid.ColumnSpan来决定其大小和位 置。
通过使用 Grid.Column 和 Grid.Row 附加属性,在 Grid 的特定单元格中定位对象。
列和行可以利用 Star 缩放来按比例分配剩余空间。当选择 Star 作为行或列的高度或宽度时, 该行或列将得到一个剩余可用空间的加权比例分配。Star 大小调整是默认行为。
示例一:
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="80"></RowDefinition>
- <RowDefinition Height="80"></RowDefinition>
- <RowDefinition Height="120"></RowDefinition>
- <RowDefinition Height="90"></RowDefinition>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="150"></ColumnDefinition>
- <ColumnDefinition></ColumnDefinition>
- </Grid.ColumnDefinitions>
- <TextBlock Name="textBlock1" Text="UserName:" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBlock>
- <TextBlock Name="textBlock2" Text="PassWord:" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBlock>
- <TextBlock Name="textBlock3" Text="Country:" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBlock>
- <TextBox Name="textBox1" Grid.Column="1" Width="200"></TextBox>
- <TextBox Name="textBox2" Grid.Column="2" Grid.Row="1" Width="200"></TextBox>
- <ListBox Grid.Column="1" Grid.Row="2">
- <ListBoxItem>中国</ListBoxItem>
- <ListBoxItem>美国</ListBoxItem>
- <ListBoxItem>英国</ListBoxItem>
- </ListBox>
- <Button Content="登录" Grid.Row="3" Grid.ColumnSpan="2" HorizontalAlignment="Center" Width="150" Height="71"></Button>
- </Grid>
效果是:
示例二:通过C#代码添加控件:
XAML代码:
- <!--LayoutRoot 是包含所有页面内容的根网格-->
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="90"/>
- <RowDefinition Height="90"/>
- <RowDefinition Height="160"/>
- <RowDefinition Height="120"/>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="150"></ColumnDefinition>
- <ColumnDefinition></ColumnDefinition>
- </Grid.ColumnDefinitions>
- </Grid>
C#代码:
- private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
- {
- TextBlock textBlock1 = new TextBlock();
- textBlock1.Text = "UserName";
- textBlock1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
- textBlock1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
- LayoutRoot.Children.Add(textBlock1);
- TextBlock textBlock2 = new TextBlock();
- textBlock2.Text = "PassWord";
- textBlock2.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
- textBlock2.VerticalAlignment = System.Windows.VerticalAlignment.Center;
- Grid.SetRow(textBlock2,1);
- LayoutRoot.Children.Add(textBlock2);
- TextBlock textBlock3 = new TextBlock();
- textBlock3.Text = "Country";
- textBlock3.VerticalAlignment = System.Windows.VerticalAlignment.Center;
- textBlock3.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
- Grid.SetRow(textBlock3,2);
- LayoutRoot.Children.Add(textBlock3);
- TextBox textBox1 = new TextBox();
- Grid.SetColumn(textBox1,1);
- textBox1.Width = 200;
- textBox1.Height = 80;
- LayoutRoot.Children.Add(textBox1);
- TextBox textBox2 = new TextBox();
- textBox2.Width = 200;
- textBox2.Height = 80;
- Grid.SetColumn(textBox2,1);
- Grid.SetRow(textBox2,1);
- LayoutRoot.Children.Add(textBox2);
- ListBox listBox1 = new ListBox();
- listBox1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
- listBox1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
- listBox1.Items.Add("北京");
- listBox1.Items.Add("上海");
- listBox1.Items.Add("杭州");
- Grid.SetRow(listBox1,2);
- Grid.SetColumn(listBox1,1);
- LayoutRoot.Children.Add(listBox1);
- Button button1 = new Button();
- button1.Content = "登录";
- button1.Width = 100;
- button1.Height = 71;
- Grid.SetRow(button1,3);
- Grid.SetColumn(button1,1);
- LayoutRoot.Children.Add(button1);
- }
效果图是:
示例三:
- <!--LayoutRoot contains the root grid where all other page content is placed-->
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
- <!--TitlePanel contains the name of the application and page title-->
- <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
- <TextBlock x:Name="ApplicationTitle" Text="SIMPLE GRID" Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock x:Name="PageTitle" Text="main page" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
- </StackPanel>
- <!--ContentPanel - place additional content here-->
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto" />
- <RowDefinition Height="*" />
- <RowDefinition Height="Auto" />
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="2*" />
- <ColumnDefinition Width="*" />
- </Grid.ColumnDefinitions>
- <TextBlock Grid.Row="0"
- Grid.Column="0"
- Grid.ColumnSpan="2"
- Text="Heading at top of Grid"
- HorizontalAlignment="Center" />
- <Image Grid.Row="1"
- Grid.Column="0"
- Source="/PhoneApp2;component/Images/1-12101Q43P9.jpg" />
- <Ellipse Grid.Row="1"
- Grid.Column="1"
- Stroke="{StaticResource PhoneAccentBrush}"
- StrokeThickness="6" />
- <TextBlock Grid.Row="2"
- Grid.Column="0"
- Grid.ColumnSpan="2"
- Text="Footer at bottom of Grid"
- HorizontalAlignment="Center" />
- </Grid>
- </Grid>
结果图: