首页 > 其他分享 >WPF 布局容器

WPF 布局容器

时间:2024-02-24 16:00:11浏览次数:24  
标签:容器 元素 布局 Dock DockPanel WPF 面板 Microsoft

参考

环境

软件/系统 版本 说明
Windows Windows 10 专业版 22H2 19045.4046
Microsoft Visual Studio Microsoft Visual Studio Community 2022 (64 位) - 17.6.5
Microsoft .Net SDK 8.0.101 手动安装
Microsoft .Net SDK 7.0.306 Microsoft Visual Studio 携带
.net 6.x 创建当前文章演示 WPF 项目时指定 .net 版本所选择的框架

正文

StackPanel

Stack 堆栈面板是 XAML 中的一个简单且有用的布局面板。 在堆栈面板中,子元素可以根据方向属性水平或垂直排列在一行中。

  1. 水平 Orientation="Horizontal"

    image

    <StackPanel Orientation="Horizontal">
    	<Button x:Name = "button0" Content = "Button" Margin = "10" Width = "120" Height = "30" />
    	<Button x:Name = "button1" Content = "Button" Margin = "10" Width = "120" Height = "30" />
    	<Button x:Name = "button2" Content = "Button" Margin = "10" Width = "120" Height = "30" />
    	<Button x:Name = "button3" Content = "Button" Margin = "10" Width = "120" Height = "30" />
    </StackPanel>
    
  2. 垂直 Orientation="Vertical"

    image

    <StackPanel Orientation="Vertical">
    	<Button x:Name = "button0" Content = "Button" Margin = "10" Width = "120" Height = "30" />
    	<Button x:Name = "button1" Content = "Button" Margin = "10" Width = "120" Height = "30" />
    	<Button x:Name = "button2" Content = "Button" Margin = "10" Width = "120" Height = "30" />
    	<Button x:Name = "button3" Content = "Button" Margin = "10" Width = "120" Height = "30" />
    </StackPanel>
    
  3. 结合 ScrollViewer 实现超出自动添加滚动条

    • 水平滚动条
      image

      		<ScrollViewer HorizontalScrollBarVisibility="Auto">
      			<StackPanel Orientation="Horizontal">
      				<Button x:Name = "button0" Content = "Button" Margin = "10" Width = "120" Height = "30" />
      				<Button x:Name = "button1" Content = "Button" Margin = "10" Width = "120" Height = "30" />
      				<Button x:Name = "button2" Content = "Button" Margin = "10" Width = "120" Height = "30" />
      				<Button x:Name = "button3" Content = "Button" Margin = "10" Width = "120" Height = "30" />
      				<Button x:Name = "button4" Content = "Button" Margin = "10" Width = "120" Height = "30" />
      				<Button x:Name = "button5" Content = "Button" Margin = "10" Width = "120" Height = "30" />
      				<Button x:Name = "button6" Content = "Button" Margin = "10" Width = "120" Height = "30" />
      			</StackPanel>
      		</ScrollViewer>
      
    • 垂直滚动条
      image

      	<ScrollViewer HorizontalScrollBarVisibility="Auto">
      		<StackPanel Orientation="Vertical">
      			<Button x:Name = "button0" Content = "Button" Margin = "10" Width = "120" Height = "30" />
      			<Button x:Name = "button1" Content = "Button" Margin = "10" Width = "120" Height = "30" />
      			<Button x:Name = "button2" Content = "Button" Margin = "10" Width = "120" Height = "30" />
      			<Button x:Name = "button3" Content = "Button" Margin = "10" Width = "120" Height = "30" />
      			<Button x:Name = "button4" Content = "Button" Margin = "10" Width = "120" Height = "30" />
      			<Button x:Name = "button5" Content = "Button" Margin = "10" Width = "120" Height = "30" />
      			<Button x:Name = "button6" Content = "Button" Margin = "10" Width = "120" Height = "30" />
      		</StackPanel>
      	</ScrollViewer>
      

WrapPanel

WrapPanel 环绕面板中,子元素根据方向属性按从左到右或从上到下的顺序排列。

  1. 上下 Orientation = "Vertical"

    image

    <WrapPanel Orientation = "Vertical">
    			<TextBlock Text = "Fist Name" Width = "60" Height = "20" Margin = "5" />
    			<TextBox  Width = "200" Height = "20" Margin = "5" />
    			<TextBlock Text = "Last Name" Width = "60" Height = "20" Margin = "5" />
    			<TextBox  Width = "200" Height = "20" Margin = "5"/>
    			<TextBlock Text = "Age" Width = "60" Height = "20" Margin = "5" />
    			<TextBox  Width = "60" Height = "20" Margin = "5" />
    			<TextBlock Text = "Title" Width = "60" Height = "20" Margin = "5" />
    			<TextBox  Width = "200" Height = "20" Margin = "5" />
    		</WrapPanel>
    
  2. 左右 Orientation = "Horizontal"

    image

    		<WrapPanel Orientation = "Horizontal">
    			<TextBlock Text = "Fist Name" Width = "60" Height = "20" Margin = "5" />
    			<TextBox  Width = "200" Height = "20" Margin = "5" />
    			<TextBlock Text = "Last Name" Width = "60" Height = "20" Margin = "5" />
    			<TextBox  Width = "200" Height = "20" Margin = "5"/>
    			<TextBlock Text = "Age" Width = "60" Height = "20" Margin = "5" />
    			<TextBox  Width = "60" Height = "20" Margin = "5" />
    			<TextBlock Text = "Title" Width = "60" Height = "20" Margin = "5" />
    			<TextBox  Width = "200" Height = "20" Margin = "5" />
    		</WrapPanel>
    

DockPanel

DockPanel 停靠面板定义一个区域,用于水平或垂直排列子元素的相对位置。 借助 DockPanel,您可以使用 Dock 属性轻松地将子元素停靠到顶部(DockPanel.Dock = "Top")、底部(DockPanel.Dock = "Bottom")、右侧(DockPanel.Dock = "Right")、左侧(DockPanel.Dock = "Left")和中心(DockPanel设置 LastChildFill = "False" 并且最后一个元素不指定 DockPanel.Dock )。

  1. 最后一个子元素始终填充剩余空间 LastChildFill = "True"

    image

    <DockPanel LastChildFill = "True"> 
    		 <Button Content = "Top" DockPanel.Dock = "Top" Click = "Click_Me" /> 
    		 <Button Content = "Bottom" DockPanel.Dock = "Bottom" Click = "Click_Me" />
    		 <Button Content = "Left" Click = "Click_Me" /> 
    		 <Button Content = "Right" DockPanel.Dock = "Right" Click = "Click_Me" /> 
    		 <Button Content = "Center" Click = "Click_Me" /> 
    	  </DockPanel> 
    
  2. 最后一个子元素停靠到另一个方向,并且还必须为最后一个子元素指定显式停靠方向 LastChildFill = "False"

    image

        <DockPanel LastChildFill = "False">
            <Button Content = "Top" DockPanel.Dock = "Top" Click = "Click_Me" />
            <Button Content = "Bottom" DockPanel.Dock = "Bottom" Click = "Click_Me" />
            <Button Content = "Left" Click = "Click_Me" />
            <Button Content = "Right" DockPanel.Dock = "Right" Click = "Click_Me" />
            <Button Content = "Center" DockPanel.Dock = "Right" Click = "Click_Me" />
        </DockPanel>
    

Grid

Grid 网格面板提供了一个由行和列组成的灵活区域。 在网格中,子元素可以以表格形式排列。

  • 3 行 2 列布局

    image

    		<Grid x:Name = "FormLayoutGrid" Background = "AliceBlue">
    			<Grid.ColumnDefinitions>
    				<ColumnDefinition Width = "Auto" />
    				<ColumnDefinition />
    			</Grid.ColumnDefinitions>
    
    			<Grid.RowDefinitions>
    				<RowDefinition Height = "*" />
    				<RowDefinition Height = "*" />
    				<RowDefinition Height = "*" />
    			</Grid.RowDefinitions>
    
    			<TextBlock Grid.Row = "0" Grid.Column = "0" Text = "Name" Margin = "10"  
    		 HorizontalAlignment = "Left" VerticalAlignment = "Center" Width = "100" />
    			<TextBox Grid.Row = "0" Grid.Column = "1" Margin = "10" />
    			<TextBlock Grid.Row = "1" Grid.Column = "0" Text = "ID" Margin = "10"  
    		 HorizontalAlignment = "Left" VerticalAlignment = "Center" Width = "100" />
    			<TextBox Grid.Row = "1" Grid.Column = "1" Margin = "10" />
    			<TextBlock Grid.Row = "2" Grid.Column = "0" Text = "Age" Margin = "10"  
    		 HorizontalAlignment = "Left" VerticalAlignment = "Center" Width = "100" />
    			<TextBox Grid.Row = "2" Grid.Column = "1" Margin = "10" />
    		</Grid>
    
    

Canvas

Canvas 画布面板是基本布局面板,可以使用相对于 Canvas 任何一侧(例如左、右、上、下)的坐标显式定位子元素。

image

        <Canvas Height="400" Width="400">
            <Canvas Height="100" Width="100" Top="0" Left="0" Background="Red"/>
            <Canvas Height="100" Width="100" Top="100" Left="100" Background="Green"/>
            <Canvas Height="100" Width="100" Top="50" Left="50" Background="Blue"/>
        </Canvas>

标签:容器,元素,布局,Dock,DockPanel,WPF,面板,Microsoft
From: https://www.cnblogs.com/xiaqiuchu/p/18031107

相关文章

  • 三列布局
    三列布局网格<style>.grid{display:grid;grid-template-columns:100pxauto100px;gap:10px;}.item{background-color:cornflowerblue;display:grid;place-items:center;}</style&......
  • 在K8S中,Pod能否实现对容器健康检查,如果服务有异常,该如何处理?
    在Kubernetes(K8S)中,Pod可以配置健康检查来监控容器的运行状态。Kubernetes提供了两种类型的健康检查:就绪探针(ReadinessProbe):就绪探针用于确定Pod中的容器是否准备好服务请求。如果探针失败,则Pod会被从对应的Service后端列表中移除,直到它通过就绪探针为止。这确保了只有健康的......
  • Kubernetes ConfigMap挂载导致容器目录中的文件被覆盖
    容器的/apps/pero/src/resources/文件夹下有多个文件,如果挂载一个文件进去,会把其他文件都覆盖掉,只剩挂载的文件按如下修改即可volumeMounts:-name:pero-configmountPath:/apps/pero/src/resources/application.propertiessubPath:a......
  • 一文带你了解容器探针
    一文带你了解容器探针马哥Linux运维 2024-02-2318:31 江苏 听全文简介容器探针(ContainerProbes)是一种机制,由kubelet对容器执行定期的探查,从而获取容器的状态。探针的类型有三种:启动探针(StartupProbe)存活探针(LivenessProbe)就绪探针(ReadinessProbe)......
  • 网状布局
    <template><div><divclass="wrap5wrap"><divclass="left"></div><divclass="center">网格布局</div><divclass="right"......
  • 绝对布局
    <template><div><divclass="wrap2wrap"><divclass="left"></div><divclass="center">绝对定位布局</div><divclass="right&quo......
  • 浮动布局
    <template><div><divclass="wrap1"><divclass="left"></div><divclass="right"></div><divclass="......
  • 表格布局
    <template><div><divclass="wrap4wrap"><divclass="left"></div><divclass="center">表格布局</div><divclass="right"......
  • flex布局
    <template><div><divclass="wrap3wrap"><divclass="left"></div><divclass="center">flex布局</div><divclass="right&quo......
  • 标签,属性与布局元素
    HTML常用元素与元素1.标签元素的功能描述html元素的功能:,双标签内容由用户提供:Helloworld单标签/空元素:没有内容也叫空元素,内容有某个属性指定,:......