扛着锄头洗澡 发表于 2023-3-21 23:20:45

WPF学习-布局

1.  Grid布局 ,(Table 布局)
两行两列布局, 
Border  0 行 0 列默认开始
<Window x:
      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:WpfApp"
      mc:Ignorable="d"
      Title="MainWindow" Height="450" Width="800">
    <Grid>
      <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
      </Grid.RowDefinitions>

      <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
      </Grid.ColumnDefinitions>

      <Border Background="Red"></Border>
      <Border Grid.Row="1" Background="Yellow"></Border>
      <Border Grid.Column="1"Background="Blue"></Border>
      <Border Grid.Row="1" Grid.Column="1" Background="Green"></Border>
    </Grid>
</Window>效果图:

 
2. StackPanel 布局
默认垂直布局 ,一旦超出区域限制后不限制
     改成水平排列
<Window x:
      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:WpfApp"
      mc:Ignorable="d"
      Title="MainWindow" Height="450" Width="800">
    <Grid>
      <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
      </Grid.RowDefinitions>

      <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
      </Grid.ColumnDefinitions>

      <Border Background="Red"></Border>
      <Border Grid.Row="1" Background="Yellow"></Border>
      <Border Grid.Column="1"Background="Blue"></Border>
      <Border Grid.Row="1" Grid.Column="1" Background="Green"></Border>
    </Grid>
</Window>                                          效果图:

3.  WrapPanel 布局, ( float布局)
默认水平排序
 
<Window x:
      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:WpfApp"
      mc:Ignorable="d"
      Title="MainWindow" Height="450" Width="800">
    <Grid>
      <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
      </Grid.RowDefinitions>

      <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
      </Grid.ColumnDefinitions>

      <Border Background="Red"></Border>
      <Border Grid.Row="1" Background="Yellow"></Border>
      <Border Grid.Column="1"Background="Blue"></Border>
      <Border Grid.Row="1" Grid.Column="1" Background="Green"></Border>
    </Grid>
</Window><Window x:
      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:WpfApp"
      mc:Ignorable="d"
      Title="MainWindow" Height="450" Width="800">
    <Grid>
      <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
      </Grid.RowDefinitions>

      <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
      </Grid.ColumnDefinitions>

      <Border Background="Red"></Border>
      <Border Grid.Row="1" Background="Yellow"></Border>
      <Border Grid.Column="1"Background="Blue"></Border>
      <Border Grid.Row="1" Grid.Column="1" Background="Green"></Border>
    </Grid>
</Window>效果:

 
4. DockPanel  停靠 (flex 布局)
默认横向填充,
    <Grid>

      <DockPanel>
            <Button Width="100" Height="40"/>
            <Button Width="100" Height="40"/>
            <Button Width="100" Height="40"/>
            <Button Width="100" Height="40"/>
            <Button Width="100" Height="40"/>
      </DockPanel>
    </Grid>效果图:
默认横向填充,  最后一个元素占据整个布局, 居中显示.

 
停靠布局
注意设置: LastChildFill="False"
    <Grid>

      <DockPanel LastChildFill="False">
            <Button Width="100" Height="40" DockPanel.Dock="Left"/>
            <Button Width="100" Height="40" DockPanel.Dock="Top"/>
            <Button Width="100" Height="40" DockPanel.Dock="Right"/>
            <Button Width="100" Height="40" DockPanel.Dock="Bottom"/>
      </DockPanel>
    </Grid>效果图:

 
5. Uniform 布局 (Table)
均分所有区域
设置三行三列布局
    <Grid>

      <DockPanel>
            <Button Width="100" Height="40"/>
            <Button Width="100" Height="40"/>
            <Button Width="100" Height="40"/>
            <Button Width="100" Height="40"/>
            <Button Width="100" Height="40"/>
      </DockPanel>
    </Grid>                            效果图:

 
6.  布局Demo 案例
Border : 类似background 属性 
<Window x:
      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:WpfApp"
      mc:Ignorable="d"
      Title="MainWindow" Height="450" Width="800">
    <Grid>
      <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
      </Grid.RowDefinitions>

      <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
      </Grid.ColumnDefinitions>

      <Border Background="Red"></Border>
      <Border Grid.Row="1" Background="Yellow"></Border>
      <Border Grid.Column="1"Background="Blue"></Border>
      <Border Grid.Row="1" Grid.Column="1" Background="Green"></Border>
    </Grid>
</Window><Window x:
      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:WpfApp"
      mc:Ignorable="d"
      Title="MainWindow" Height="450" Width="800">
    <Grid>
      <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
      </Grid.RowDefinitions>

      <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
      </Grid.ColumnDefinitions>

      <Border Background="Red"></Border>
      <Border Grid.Row="1" Background="Yellow"></Border>
      <Border Grid.Column="1"Background="Blue"></Border>
      <Border Grid.Row="1" Grid.Column="1" Background="Green"></Border>
    </Grid>
</Window><Window x:
      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:WpfApp"
      mc:Ignorable="d"
      Title="MainWindow" Height="450" Width="800">
    <Grid>
      <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
      </Grid.RowDefinitions>

      <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
      </Grid.ColumnDefinitions>

      <Border Background="Red"></Border>
      <Border Grid.Row="1" Background="Yellow"></Border>
      <Border Grid.Column="1"Background="Blue"></Border>
      <Border Grid.Row="1" Grid.Column="1" Background="Green"></Border>
    </Grid>
</Window><Window x:
      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:WpfApp"
      mc:Ignorable="d"
      Title="MainWindow" Height="450" Width="800">
    <Grid>
      <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
      </Grid.RowDefinitions>

      <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
      </Grid.ColumnDefinitions>

      <Border Background="Red"></Border>
      <Border Grid.Row="1" Background="Yellow"></Border>
      <Border Grid.Column="1"Background="Blue"></Border>
      <Border Grid.Row="1" Grid.Column="1" Background="Green"></Border>
    </Grid>
</Window>    <Grid>

      <DockPanel>
            <Button Width="100" Height="40"/>
            <Button Width="100" Height="40"/>
            <Button Width="100" Height="40"/>
            <Button Width="100" Height="40"/>
            <Button Width="100" Height="40"/>
      </DockPanel>
    </Grid>                        

来源:https://www.cnblogs.com/voidobject/archive/2023/03/21/17241758.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: WPF学习-布局