WPF当中有许多的布局容器控件,例如<Grid>、<StackPanel>、<WrapPanel>、<DockPanel>、<UniformGrid>。接下来分别介绍一下各个布局容器控件。

布局基础

Grid

<Grid><Grid.RowDefinitions><RowDefinition Height="2*"/><RowDefinition/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="2*"/><ColumnDefinition/></Grid.ColumnDefinitions><Border Grid.Row="0" Grid.Column="0" Background="Red"/><Border Grid.Row="0" Grid.Column="1" Background="Yellow"/><Border Grid.Row="1" Grid.Column="0" Background="Blue"/><Border Grid.Row="1" Grid.Column="1" Background="Green"/>
</Grid>

Grid中的元素还可以跨行和跨列:

StackPanel

StackPanel默认水平排列,具有 Orientation=""属性可以改变排列方向。

WrapPanel

WrapPanel默认水平排列,也具有 Orientation=""属性可以改变排列方向。

DockPanel

DockPanel具有停靠的功能,位于DockPanel中的元素,可以设置它的方向。

UniformGrid

这个容器最大的作用就是在有限的空间里面均分剩余空间。

下面对一个页面进行布局模拟练习:

<Grid><Grid.RowDefinitions><RowDefinition Height="80"/><RowDefinition/></Grid.RowDefinitions><Border Background="#FFECF1"/><Grid Grid.Row="1"><Grid.ColumnDefinitions><ColumnDefinition Width="200"/><ColumnDefinition/></Grid.ColumnDefinitions><Border Background="#FF7F24"/><Grid Grid.Column="1" Margin="5"><Grid.RowDefinitions><RowDefinition Height="100"/><RowDefinition/><RowDefinition/></Grid.RowDefinitions><Grid Grid.Row="0"><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Border Margin="5" Grid.Column="0" Background="#1b1c1d"/><Border Margin="5" Grid.Column="1" Background="#2AC864"/><Border Margin="5" Grid.Column="2" Background="#1b1c1d"/><Border Margin="5" Grid.Column="3" Background="#F85A54"/><Border Margin="5" Grid.Column="4" Background="#1b1c1d"/></Grid><Grid Grid.Row="1"><Grid.ColumnDefinitions><ColumnDefinition Width="1.5*"/><ColumnDefinition/></Grid.ColumnDefinitions><Border Margin="5" Grid.Column="0" Background="#1b1c1d"/><Border Margin="5" Grid.Column="1" Background="#2AC864"/></Grid><Grid Grid.Row="2"><Grid.ColumnDefinitions><ColumnDefinition Width="1.5*"/><ColumnDefinition/></Grid.ColumnDefinitions><Border Margin="5" Grid.Column="0" Background="#1b1c1d"/><Border Margin="5" Grid.Column="1" Background="#F85A54"/></Grid></Grid></Grid>
</Grid>

样式基础

样式负责修饰元素的外观以及行为,可以在样式中定义不同类型元素的属性值集合。

<Window.Resources><Style x:Key="BaseStyle" TargetType="Button"><Setter Property="FontSize" Value="18"/><Setter Property="Foreground" Value="White"/><Setter Property="Background" Value="Red"/></Style><Style x:Key="BottonStyle" TargetType="Button" BasedOn="{StaticResource BaseStyle}"><Setter Property="Content" Value="Botton1"/></Style>
</Window.Resources>
<Grid><StackPanel><Button Background="Blue" Style="{StaticResource BottonStyle}"/><Button Style="{StaticResource BottonStyle}"/><Button Style="{StaticResource BottonStyle}"/></StackPanel>
</Grid>

数据模板

<Grid><ListBox x:Name="list"><ListBoxItem><StackPanel Orientation="Horizontal"><Border Width="10" Height="10" Background="Red"/><TextBlock Margin="10,0"  Text="Red"/></StackPanel></ListBoxItem><ListBoxItem><StackPanel Orientation="Horizontal"><Border Width="10" Height="10" Background="Blue"/><TextBlock Margin="10,0"  Text="Red"/></StackPanel></ListBoxItem><ListBoxItem><StackPanel Orientation="Horizontal"><Border Width="10" Height="10" Background="Green"/><TextBlock Margin="10,0"  Text="Red"/></StackPanel></ListBoxItem></ListBox>
</Grid>

可以看到,这里每一项的构造都是完全相同的,唯一不同的就是它们的数据,因此可以将它抽离出来作为一个模板。

<Grid><ListBox x:Name="list"><ListBox.ItemTemplate><DataTemplate><StackPanel Orientation="Horizontal"><Border Width="10" Height="10" Background="{Binding Code}"/><TextBlock Margin="10,0"  Text="{Binding Name}"/></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox>
</Grid>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WpfApp4 {/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window {public MainWindow() {InitializeComponent();List<Color> test = new List<Color>();test.Add(new Color() { Code = "#FFB6C1", Name = "浅粉红" });test.Add(new Color() { Code = "#FFC0CB", Name = "粉红" });test.Add(new Color() { Code = "#DC143C", Name = "猩红" });test.Add(new Color() { Code = "#FFF0F5", Name = "脸红的淡紫色" });//LightPink 浅粉红	#FFB6C1	255,182,193//Pink 粉红	#FFC0CB	255,192,203//Crimson 猩红	#DC143C	220,20,60//LavenderBlush 脸红的淡紫色	#FFF0F5	255,240,245list.ItemsSource = test;}public class Color {public string Code { get; set; }public string Name { get; set; }}}
}

由此就可以看到DataTemplate的作用。继续编写例子:

<Grid><DataGrid x:Name = "grid" AutoGenerateColumns="False" CanUserAddRows="False"><DataGrid.Columns><DataGridTextColumn Header="Code" Binding="{Binding Code}"/><DataGridTextColumn Header="Name" Binding="{Binding Name}"/><DataGridTemplateColumn Header="操作"><DataGridTemplateColumn.CellTemplate><DataTemplate><StackPanel Orientation="Horizontal"><Button Content="删除" /><Button Content="复制" /><Button Content="保存" /></StackPanel></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn></DataGrid.Columns></DataGrid>
</Grid>

灵活地使用DataTemplate数据模块,对后续开发有很重要的帮助,当然DataTemplate的应用不止于此,后续还会介绍到。

绑定

使用绑定可以使得控件与控件之间建立一种绑定关系,可以不再为此编写大量的代码来维持。

<Grid><StackPanel><Slider x:Name="slider" Margin="5" ValueChanged="Slider_ValueChanged"/><TextBox x:Name="textbox1" Margin="5" Height="30" TextChanged="textbox1_TextChanged"/><TextBox x:Name="textbox2" Margin="5" Height="30"/><TextBox x:Name="textbox3" Margin="5" Height="30"/></StackPanel>
</Grid>
namespace WpfApp5 {/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window {public MainWindow() {InitializeComponent();}private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {textbox1.Text = slider.Value.ToString();textbox2.Text = slider.Value.ToString();textbox3.Text = slider.Value.ToString();}private void textbox1_TextChanged(object sender, TextChangedEventArgs e) {if (double.TryParse(textbox1.Text, out double result))slider.Value = result;}}       
}

此时代码实现了拖动滑块,下方三个框里的数据会同步变化。对下方任意一个框里的数据进行修改,上方滑块的位置也会同步变化。为了实现这个功能,代码变得特别的冗余,可以使用绑定来简化。

<Grid><StackPanel><Slider x:Name="slider" Margin="5"/><TextBox Text="{Binding ElementName=slider,Path=Value}" x:Name="textbox1" Margin="5" Height="30"/><TextBox Text="{Binding ElementName=slider,Path=Value}" x:Name="textbox2" Margin="5" Height="30"/><TextBox Text="{Binding ElementName=slider,Path=Value}" x:Name="textbox3" Margin="5" Height="30"/></StackPanel>
</Grid>

此时就不需要具体的实现代码了,使用绑定就可以完成两个控件的双向绑定。 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/diannao/87555.shtml
繁体地址,请注明出处:http://hk.pswp.cn/diannao/87555.shtml
英文地址,请注明出处:http://en.pswp.cn/diannao/87555.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

开源大型语言模型的文本记忆新突破!

在现代科技的推动下&#xff0c;人工智能领域正在不断地突破人类认知的极限。今年&#xff0c;由斯坦福大学、康奈尔大学和西弗吉尼亚大学的计算机科学家们&#xff0c;与法律学者共同展开了一项引人入胜的研究&#xff0c;聚焦于开源大型语言模型的文本记忆表现。这项研究不仅…

LeetCode 3090.每个字符最多出现两次的最长子字符串

题目链接 https://leetcode.cn/problems/maximum-length-substring-with-two-occurrences/ 题目描述 给定一个字符串 s&#xff0c;找出满足每个字符最多出现两次的最长子字符串&#xff0c;并返回其长度。 示例 输入&#xff1a;s "aabba" 输出&#xff1a;5解…

使用开源NVIDIA cuOpt加速决策优化

使用开源NVIDIA cuOpt加速决策优化 文章目录 使用开源NVIDIA cuOpt加速决策优化决策优化的现实挑战供应链优化的复杂性实时决策的挑战计算复杂性的挑战 NVIDIA cuOpt&#xff1a;GPU加速的决策优化解决方案cuOpt的核心技术架构支持的优化问题类型性能优势分析 实际应用案例&…

【JVM 09-垃圾回收】

垃圾回收 笔记记录 1. 如何判断对象可以回收1.1 引用计数法1.1.1 缺点 1.2 可达性分析算法1.2.1 可达分析、根对象1.2.2 优缺点 1.3 四种引用(强软弱虚)1.3.1 软引用的实际使用案例1.3.2 软引用-引用队列1.3.3 弱引用的实际使用案例 2. 垃圾回收算法2.1 标记清除算法2.2 标记整…

《二叉搜索树》

引言&#xff1a; 上次我们结束了类和对象的收尾&#xff0c;之后我们就要学习一些高级的数据结构&#xff0c;今天我们先来看一个数据结构-- 二叉搜索树。 一&#xff1a; 二叉搜索树的概念(性质) 二叉搜索树又称二叉排序树&#xff0c;它或者是一棵空树&#xff0c;或者是…

【Redis】Sentinel哨兵

&#x1f6e1;️ 深入理解 Redis Sentinel&#xff1a;高可用架构的守护者 在实际开发中&#xff0c;我们常用 Redis 构建缓存系统或数据中间件。然而&#xff0c;主从复制虽然能实现数据同步&#xff0c;但无法自动故障转移&#xff08;failover&#xff09;&#xff0c;这就…

Shell脚本应用及实战演练

文章目录 一、Shell脚本语言的基本结构1、Shell脚本的用途&#xff1a;2、 Shell脚本基本结构&#xff1a;3、 创建Shell脚本过程4、 脚本注释规范 二、Shell脚本语言的变量用法详解位置与预定义变量 三、 Shell字符串详解1、Shell字符串拼接2、Shell字符串截取3、 Shell的格式…

软件工程瀑布模型学习指南

软件工程瀑布模型学习指南 一、瀑布模型核心概念 1.1 定义与特点 瀑布模型是一种经典的软件开发流程,将项目划分为顺序性的阶段,每个阶段有明确的输入和输出,如同瀑布流水般单向推进。其特点包括: 阶段间具有明确的顺序性和依赖性强调文档驱动和阶段评审适合需求明确、稳…

获取gitlab上项目分支版本(二)

获取gitlab上项目分支版本_gitlab代码分支版本在哪-CSDN博客 原先写过一版&#xff0c;但是这次想更新一下项目的分支信息时&#xff0c;提示我 git服务器上的Python版本是2.7.3&#xff0c;这个错误表明当前Python环境中没有安装requests库&#xff0c;服务器也没有连接外网&…

主流防火墙策略绕过漏洞的修复方案与加固实践

主流防火墙策略绕过漏洞的修复方案与加固实践 流量关键点分析&#xff08;攻击手法&#xff09; 攻击者通过精心构造的TCP序列号攻击和恶意标志组合绕过防火墙DPI检测&#xff0c;核心手法如下&#xff1a; TCP连接建立&#xff08;正常握手&#xff09; 1049&#xff1a;客户…

泛微OAe9-后端二开常见数据库操作

泛微OAe9-后端二开常见数据库操作 文章目录 泛微OAe9-后端二开常见数据库操作一、RecordSet1 RecordSet 操作OA本身的表2 RecordSet 操作OA 本身的存储过程 二、RecordSetTrans三、RecordSetDataSource四、原生 jdbc 一、RecordSet RecordSet 适用于操作 OA 自己的库。OA 数据库…

【数据分析八:hypothesis testing】假设检验

本节我们讲述假设检验和抽样方法 有关假设检验的详细内容&#xff0c;可以参考我以往的博客 概率论与数理统计总复习_概率论与数理统计复习-CSDN博客文章浏览阅读1.5k次&#xff0c;点赞33次&#xff0c;收藏23次。中科大使用的教辅《概率论和数理统计》&#xff0c;带大家复…

AI免费工具:promptpilot、今天学点啥、中英文翻译

promptpilot 激发模型潜能&#xff0c;轻松优化 Prompt https://promptpilot.volcengine.com/startup 今天学点啥 https://metaso.cn/study 能生成网页和语音播报 中英文翻译 沉浸式翻译&#xff0c;浏览器插件&#xff0c;ai翻译

计算机网络学习笔记:TCP三报文握手、四报文挥手

文章目录 前言一、TCP三报文握手二、TCP四报文挥手三、TCP保活计时器 前言 TCP通信&#xff0c;通常需要经历三个阶段&#xff1a;三报文握手->发送&#xff0c;接收数据->四报文挥手。 一、TCP三报文握手 三报文握手处于TCP的连接建立阶段&#xff0c;主要解决了以下的…

kafka部署和基本操作

一、部署kafka 解压 tar xzvf kafka_2.12-3.9.1.tgz tar -zxf kafka_2.12-3.9.1.tgz 1.修改config/server.properties # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # …

Bootstrap 5学习教程,从入门到精通,Bootstrap 5 导航语法知识点及案例代码(17)

Bootstrap 5 导航语法知识点及案例代码 Bootstrap 5 提供了强大的导航组件&#xff0c;帮助开发者快速构建响应式且美观的导航栏。 一、Bootstrap 5 导航组件概述 Bootstrap 5 提供了多种导航组件&#xff0c;主要包括&#xff1a; 导航栏&#xff08;Navbar&#xff09;&am…

清除 docker 无用的 镜像/容器

清除 docker 无用的 镜像/容器 删除 <none> 的 docker 镜像 使用以下命令删除所有 的 Docker 镜像&#xff08;即悬空镜像 / dangling images&#xff09;&#xff1a; docker image prune -f这会自动删除所有没有 tag 的镜像&#xff08;&#xff09;&#xff0c;不会…

使用Charles抓包工具提升API调试与性能优化效率

在软件开发过程中&#xff0c;网络请求调试和性能优化往往成为开发者遇到的挑战&#xff0c;尤其是在进行API接口调试时。开发者需要确保网络请求的正确性、响应时间以及系统的整体性能。然而&#xff0c;传统的调试方法常常无法提供足够的细节来深入分析问题&#xff0c;进而影…

如何协调各项目关键节点的冲突与依赖

在多项目并行的环境下&#xff0c;关键节点间的冲突与依赖是导致项目延期、资源浪费和沟通误解的主要根源。要高效协调此类问题&#xff0c;企业应重点从建立透明的进度依赖图、使用项目管理工具对齐节点、推动跨部门协同机制入手。其中&#xff0c;通过Gantt图或关键路径法实现…

mongodb单节点改副本集模式

前一阵将三节点的副本集改成了单节点&#xff0c;但后面业务代码出现问题&#xff1a;无法使用事务&#xff0c;因为事务只有在副本集上能用&#xff0c;单节点无法使用&#xff0c;故需要改回副本集模式&#xff0c;而我目前仅有一台服务器&#xff0c;所以考虑在一台服务器上…