WPF - 布局嵌套

  • 简述

    布局嵌套意味着在另一个布局中使用布局面板,例如在网格内定义堆栈面板。这个概念被广泛用于在应用程序中利用多种布局的优势。在下面的示例中,我们将在网格内使用堆栈面板。
    让我们看一下下面的 XAML 代码。
    
    <Window x:Class = "WPFNestingLayouts.MainWindow" 
       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" 
       xmlns:local = "clr-namespace:WPFNestingLayouts" 
       mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">
         
       <Grid Background = "AntiqueWhite"> 
          <Grid.RowDefinitions> 
             <RowDefinition Height = "*" /> 
             <RowDefinition Height = "*" /> 
             <RowDefinition Height = "*" /> 
             <RowDefinition Height = "*" /> 
             <RowDefinition Height = "*" /> 
          </Grid.RowDefinitions> 
              
          <Grid.ColumnDefinitions> 
             <ColumnDefinition Width = "*" /> 
          </Grid.ColumnDefinitions> 
              
          <Label Content = "Employee Info" FontSize = "15"
             FontWeight = "Bold" Grid.Column = "0" Grid.Row = "0"/> 
                   
          <StackPanel Grid.Column = "0" Grid.Row = "1" Orientation = "Horizontal"> 
             <Label Content = "Name"  VerticalMoognment = "Center" Width = "70"/> 
             <TextBox Name = "txtName" Text = "Muhammad Moo" VerticalMoognment = "Center"
                Width = "200">
             </TextBox> 
          </StackPanel>
              
          <StackPanel Grid.Column = "0" Grid.Row = "2" Orientation = "Horizontal"> 
             <Label Content = "ID" VerticalMoognment = "Center" Width = "70"/> 
             <TextBox Name = "txtCity" Text = "421" VerticalMoognment = "Center"
                Width = "50">
             </TextBox> 
          </StackPanel>
              
          <StackPanel Grid.Column = "0" Grid.Row = "3" Orientation = "Horizontal"> 
             <Label Content = "Age" VerticalMoognment = "Center" Width = "70"/> 
             <TextBox Name = "txtState" Text = "32" VerticalMoognment = "Center"
                Width = "50"></TextBox> 
          </StackPanel> 
              
          <StackPanel Grid.Column = "0" Grid.Row = "4" Orientation = "Horizontal"> 
             <Label Content = "Title" VerticalMoognment = "Center" Width = "70"/> 
             <TextBox Name = "txtCountry" Text = "Programmer" VerticalMoognment = "Center"
                Width = "200"></TextBox> 
          </StackPanel> 
              
       </Grid> 
         
    </Window>            
    
    当你编译并执行上面的代码时,它会产生如下的窗口。
    布局嵌套的输出
    我们建议您执行上述示例代码并尝试其他嵌套布局。