WPF佈局示例
<Window x:Class="佈局.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:佈局"
mc:Ignorable="d"
Title="MainWindow" Height="450" MinWidth="300" MaxWidth="800" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*" MinWidth="100"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" HorizontalContentAlignment="Right" Margin="3" FontSize="20">Home:</Label>
<TextBox Name="HomeTxt" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Height="Auto"></TextBox>
<Button Name="HomBtn" Grid.Row="0" Grid.Column="2" Margin="3" Padding="2" Height="Auto" BorderBrush="LightBlue" BorderThickness="3" Click="HomBtn_Click">Browse</Button>
<Label Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalContentAlignment="Right" Margin="3" FontSize="20">NetWork:</Label>
<TextBox Name="NetWorkTxt" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" Height="Auto"></TextBox>
<Button Name="NetWorkBtn" Grid.Row="1" Grid.Column="2" Margin="3" Padding="2" Height="Auto" BorderBrush="LightBlue" BorderThickness="3">Browse</Button>
<Label Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" HorizontalContentAlignment="Right" Margin="3" FontSize="20">Web:</Label>
<TextBox Name="WebTxt" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" Height="Auto"></TextBox>
<Button Name="WebBtn" Grid.Row="2" Grid.Column="2" Margin="3" Padding="2" Height="Auto" BorderBrush="LightBlue" BorderThickness="3">Browse</Button>
<Label Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" HorizontalContentAlignment="Right" Margin="3" FontSize="20">Secondary:</Label>
<TextBox Name="SecondaryTxt" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center" Height="Auto"></TextBox>
<Button Name="SecondaryBtn" Grid.Row="3" Grid.Column="2" Margin="3" Padding="2" Height="Auto" BorderBrush="LightBlue" BorderThickness="3">Browse</Button>
</Grid>
</Window>
界面效果如下:
說明:
中間的textbox的大小可以跟着窗體的大小變化,左邊的Label和右邊的Button的大小不會變
- 參考《WPF編程寶典佈局示例》