首页 > 其他分享 >WPF布局

WPF布局

时间:2024-08-03 11:33:42浏览次数:10  
标签:控件 布局 占用 元素 DockPanel Grid WPF

在 WPF 中,StackPanel 是一个非常常用的布局控件,它会按照指定的方向(水平或垂直)依次排列子元素。然而,StackPanel 本身并不提供直接的方法来让最后一个子元素占用剩余空间。然而,可以通过一些变通的方法来实现这一点。

以下两种方法可以实现让 StackPanel 中的最后一个元素占用剩余空间的效果:

方法一:使用 DockPanel

利用 DockPanel 可以实现类似于 StackPanel 的布局,同时还能指定某个子元素占用剩余的空间。您可以将子元素依次设置为靠上(或左),并将最后一个元素设置为占用剩余空间。

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel LastChildFill="True">
        <!-- 其他子元素 -->
        <Button Content="Button 1" DockPanel.Dock="Top"/>
        <Button Content="Button 2" DockPanel.Dock="Top"/>
        
        <!-- 最后一个子元素 -->
        <Button Content="Button 3"/>
    </DockPanel>
</Window>

在这个示例中:

子元素 Button 1 和 Button 2 依次停靠在顶部(DockPanel.Dock="Top")。
DockPanel 的 LastChildFill="True" 属性确保最后一个子元素(Button 3)占用剩余的所有空间。

方法二:使用 Grid

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <!-- 定义三行 -->
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/> <!-- 自动高度 -->
            <RowDefinition Height="Auto"/> <!-- 自动高度 -->
            <RowDefinition Height="*"/>    <!-- 剩余空间 -->
        </Grid.RowDefinitions>

        <!-- 添加一些内容到Grid -->
        <Button Content="Button 1" Grid.Row="0"/>
        <Button Content="Button 2" Grid.Row="1"/>
        <Button Content="Button 3" Grid.Row="2"/>
    </Grid>
</Window>

在这个示例中:

前两行的高度设置为 Auto,根据内容自动调整高度。
最后一行的高度设置为 *,表示它将占用剩余的所有空间。

在 WPF 中,如果你希望一个子控件的高度(或宽度)与其父控件的高度(或宽度)一致,可以使用以下几种方法

方法一:使用 VerticalAlignment 和 HorizontalAlignment
通过设置控件的 VerticalAlignment 和 HorizontalAlignment 属性为 Stretch,控件会填充整个父容器的空间。

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <!-- 这个Button会占据整个Grid的高度和宽度 -->
        <Button Content="Button" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
    </Grid>
</Window>

方法二:使用 Grid 布局
在 Grid 中,如果只有一个子元素并且该子元素的 HorizontalAlignment 和 VerticalAlignment 属性设置为 Stretch,默认情况下该子元素会填充整个 Grid。

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <!-- 这个Button会占据

标签:控件,布局,占用,元素,DockPanel,Grid,WPF
From: https://www.cnblogs.com/JosenEarth/p/18340216

相关文章

  • WPF C# implement scaletransform and translatetransfrom programmatically
    privatevoidInitRenderTransfrom(){TransformGrouptg=newTransformGroup();ScaleTransformst=newScaleTransform();if(!tg.Children.Contains(st)){tg.Children.Add(st);scaler=st;}TranslateTransformtt=n......
  • 【C#】WPF实现HaIcon图像缩放、移动
    1.HaIcon实现的C#代码////FilegeneratedbyHDevelopforHALCON/DOTNET(C#)Version12.0////ThisfileisintendedtobeusedwiththeHDevelopTemplateor//HDevelopTemplateWPFprojectslocatedunder%HALCONEXAMPLES%\c#usingSystem;usingSystem.Thre......
  • WPF 自定义对话框
    <Windowx:Class="WPFDemo2.窗体.CustomDialogWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas......
  • 【C#】WPF自定义Image实现图像缩放、平移
    1.xaml实现<UserControlx:Class="HalconDemo.ImageUserControl"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://sche......
  • Android 11.0 Launcher修改density禁止布局改变功能实现
    1.前言在11.0的系统rom定制化开发中,在关于Launcher3的定制化功能中,在有些功能需要要求改变系统原有的density屏幕密度,这样就会造成Launcher3的布局变化,所以就不符合要求,接下来就来看下如何禁止改变density造成Launcher3布局功能改变的实现2.Launcher修改density禁止布局改......
  • 《深入浅出WPF》学习笔记三.x命名空间以及常见属性
    《深入浅出WPF》学习笔记三.x命名空间以及常见属性X命名空间的由来和作用xaml:是eXtensibleApplicationMarkupLanguage的英文缩写(可扩展应用程序标记语言);声明       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"使用x:Class="WpfApp10.Main......
  • wpf基础
    在WPF(WindowsPresentationFoundation)中,Style是一种强大的资源,允许你定义一组属性值,这些值可以被多个控件实例共享。使用Style可以减少重复的XAML代码,并且使得UI的一致性和可维护性得到提高。以下是一些Style的基本概念和用法:定义Style你可以在XAML中的......
  • 一个div 使用flex 布局2个div,第一个div占75%,另一个占25%
    <divclass="container"><divclass="childchild-75">第一个div</div><divclass="childchild-25">第二个div</div></div>.container{display:flex;/*启用Flexbox*/width:100%;/*假设容器占满整......
  • QT之ui控件随窗口布局的大小而自适应大小
    QT之ui窗口自适应布局新建个工程说明,注意此处勾上Generateform根据开发电脑的系统选择套件点开widget.ui,如图鼠标随意托几个常用控件展示,如图三个控件,如图,水平布局sizePolicy策略:图中,控件的sizePolicy策略将决定上面这三个控件组在自适应成的控件组的分配策略。Fi......
  • 一个wpf项目的搭建prism框架mvvm
    一个wpf项目的搭建prism框架mvvm简单prism项目:1.新建一个wpf的项目2.引入包:在nuget中,prism.DryIoc3.把空项目应用转化成Prism,把App基类Application改为PrismApplication,因为这个基类是分布类,其中app.xaml.cs基类改为PrismApplication,和xaml的标签,引入命名空间后改为<prism......