C# WPF:初識布局容器

StackPanel堆疊布局

StackPanel是簡單布局方式之一,可以很方便的進行縱向布局和橫向布局 StackPanel默認是縱向布局的

復制代碼

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow"> 
<StackPanel> 
<Button Content="按鈕"></Button>
<Button Content="按鈕"></Button>
<Button Content="按鈕"></Button>
<Button Content="按鈕"></Button>
<Label Content="Label"></Label>
<Label Content="Label"></Label>
<Label Content="Label"></Label>
</StackPanel> 
</Window>

復制代碼

如果要橫向布局的話,只要把StackPanel的Orientation屬性設置成Horizontal即可

這個屬性的默認值是Vertical

復制代碼

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow"> 
<StackPanel Orientation="Horizontal"> 
<Button Content="按鈕"></Button>
<Button Content="按鈕"></Button>
<Button Content="按鈕"></Button>
<Button Content="按鈕"></Button>
<Label Content="Label"></Label>
<Label Content="Label"></Label>
<Label Content="Label"></Label>
</StackPanel> 
</Window>

復制代碼

WrapPanel包裹布局
在WrapPanel面板中的元素以一次一行或一列的方式布局控件
WrapPanel也有Orientation屬性,但與StackPanel不同的是,WrapPanel的Orientation屬性的默認值是Horizontal
也就是說WrapPanel的默認展現方向是橫向的
WrapPanel與StackPanel另一個不同的地方是,當容器實際寬度不夠的情況下,內容將以多行或者多列的形式展現

復制代碼

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow"> 
<WrapPanel>
<Button Content="allen"></Button>
<Button Content="allen"></Button>
<Button Content="allen"></Button>
<Button Content="allen"></Button>
<Button Content="allen"></Button>
<Button Content="allen"></Button>
</WrapPanel>
</Window>

復制代碼

?WrapPanel的縱向展現方式

復制代碼

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow"> 
<WrapPanel Orientation="Vertical">
<Button Content="allen1"></Button>
<Button Content="allen2"></Button>
<Button Content="allen3"></Button>
<Button Content="allen4"></Button>
<Button Content="allen5"></Button>
<Button Content="allen6"></Button>
<Button Content="allen7"></Button>
<Button Content="allen8"></Button>
<Button Content="allen9"></Button>
<Button Content="allen10"></Button>
</WrapPanel>
</Window>

復制代碼

DockPanel停靠布局
這種布局把布局容器分為上、下、左、右四個邊緣,容器內的元素沿著某一個邊緣來拉伸自己

復制代碼

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow"> 
<DockPanel>
<!--沿著上邊緣拉伸-->
<Button Content="Top" DockPanel.Dock="Top"></Button>
<!--沿著下邊緣拉伸-->
<Button Content="Bottom" DockPanel.Dock="Bottom"></Button>
<!--沿著左邊緣拉伸-->
<Button Content="Left" DockPanel.Dock="Left"></Button>
<!--沿著右邊緣拉伸-->
<Button Content="Right" DockPanel.Dock="Right"></Button>
<!--默認沿著左邊緣拉伸-->
<Button Content="allen5"></Button>
<!--默認沿著左邊緣拉伸-->
<Button Content="allen6"></Button>
<!--最后一個元素默認填充滿整個容器剩余的空間-->
<Button Content="默認最后一個自適應"></Button>
</DockPanel>
</Window>

復制代碼

Grid表格布局
Grid布局容器可以把空間分割成多行多列,用以擺放不同的控件

復制代碼

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow"> 
<Grid>
<!--定義兩行-->
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<!--定義三列-->
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--Grid.Row或 Grid.Column的默認值為0-->
<Button Content="默認在第一行第一列且填充"></Button>
<!--如果我把Grid.Row的值設置成2,因為沒有第三行,所以按鈕會自動被放在最后一行,仍然是第二行-->
<Button Grid.Row="1" Grid.Column="1" Content="第二行第二列"></Button>
</Grid>
</Window>

復制代碼

Canvas畫布布局
Canvas畫布布局容器允許使用精確的坐標來擺放畫布內的元素
如果兩個元素共用了同一塊區域,那么后設置的元素將覆蓋先設置的元素

復制代碼

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow"> 
<Canvas>
<Button Canvas.Left="100" Canvas.Top="100" Content="第一個按鈕"></Button>
<Button Canvas.Left="136" Canvas.Top="112" Content="第二個按鈕"></Button>
</Canvas>
</Window>

復制代碼

Window窗口
窗口是容納所有WPF界面元素的最初容器,任何的界面元素都要放在Window窗口內才能呈現
WPF窗口只能包含一個兒子控件,這是因為Window類繼承自ContentControl類。

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<!--你不能在這里放置多個同級元素-->
</Window>

ContentControl就是我們常說的內容控件,這種控件與容器控件(Grid或StackPanel)不同,
內容控件的頂級子元素只能有一個,容器控件可以包含多個頂級子元素
如果我們想要在一個ContentControl內展示多個子控件,
我們可以先放置一個容器控件作為內容控件的頂級子元素,然后再在此容器控件中放置更多的控件

復制代碼

<Window x:Class="WpfApplication1.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 Content="Button" />
<Button Content="Button" />
</Grid>
</Window>

復制代碼

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/252066.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/252066.shtml
英文地址,請注明出處:http://en.pswp.cn/news/252066.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Kibana源碼分析--Hapijs路由設置理解筆記

【ES6解構賦值】&#xff1a;https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment 【Joi APi】&#xff1a;https://github.com/hapijs/joi/blob/v13.1.2/API.md 轉載于:https://www.cnblogs.com/lishidefengchen/p/866874…

Python打包EXE神器 pyinstaller

最近由于項目需要&#xff0c;以前的python文件需要編輯為EXE供前端客戶使用。 由于最早接觸的是distutils&#xff0c;所以一開始準備使用distutils和py2exe搭配來進行python的exe化&#xff0c;也就是傳統的使用setup.py的方式來進行exe安裝。但是結果都不是很好&#xff0c;…

好程序員HTML5前端教程-css的引入方式和選擇器

好程序員HTML5前端教程-css的引入方式和選擇器 01.引入css方式&#xff08;重點掌握&#xff09; 行內樣式 內接樣式 外接樣式      3.1 鏈接式      3.1 導入式 css介紹 現在的互聯網前端分三層&#xff1a; HTML&#xff1a;超文本標記語言。從語義的角度描述頁面結…

4.4.6 數組也能無鎖:AtomicIntegerArray

數組也可以實現cas操作&#xff0c;有以下幾個類以及用法如下&#xff1a; public class AtomicTntegerArrayTest {public static void main(String[] args) {AtomicIntegerArray atomicIntegerArraynew AtomicIntegerArray(3);AtomicLongArray atomicIntegerArray1new AtomicL…

20種PLC元件編號和Modbus編號地址對應表

1、三菱&#xff1a; X元件支持Modbus之02功能碼&#xff1b; Y元件支持Modbus之01、05、15功能碼&#xff1b; D元件支持Modbus之03、06、16功能碼。 2、西門子&#xff1a; I元件支持Modbus之02功能碼&#xff1b; Q元件支持Modbus之01、05、15功能碼&#xff1b; V元件…

暑期學習

由于最后大作業的呈現情況與短學期所完成的還相差甚遠&#xff0c;所以在暑期的時候開始進一步的細化。 在這個過程之中產生了如下的問題&#xff1a; 已解決的有&#xff1a; 1.用a標簽在同一頁面實現跳轉。 要點&#xff1a;標記<a href"../home#pre">的時候…

五、RabbitMQ的消息屬性(讀書筆記)

2019獨角獸企業重金招聘Python工程師標準>>> 簡介 當使用RabbitMQ發布消息時&#xff0c;消息又AMQP規范中的三個低層幀類型組成&#xff1a; Basic.publish方法幀&#xff1b;內容頭幀&#xff1b;消息體幀&#xff1b;這三種幀類型按順序一起工作&#xff0c;以便…

異步和單線程

轉載于:https://www.cnblogs.com/sunmarvell/p/8674748.html

windows下解決mysql5中文亂碼的問題

1.問題描述&#xff1a;一開始無論是在命令行&#xff0c;還是在mysql的客戶端輸入中文都會出現 “???” 問題之類的亂碼問題&#xff1b; 2.解決辦法&#xff1a; 1&#xff09;cmd 進入mysql &#xff0c;命令mysql -uroot -p123456 2&#xff09;然后執行 show variable…

C#:把dll封入exe中方法

在這個事件中,可以重新為加載失敗的程序集手動加載 如果你將dll作為資源文件打包的你的應用程序中(或者類庫中) 就可以在硬盤加載失敗的時候 從資源文件中加載對應的dll 就像這樣: class Program {static Program(){ //這個綁定事件必須要在引用到TestLibrary1這個程序…

P2685 [TJOI2012]橋

P2685 [TJOI2012]橋 思路&#xff1a; 先求出最短路&#xff1a; d1[u] : u 到 1 的最短路&#xff0c; d2[u] : u 到 n 的最短路 再求出一條從 1 到 n 的最短路鏈&#xff0c;然后從鏈上的每一個點出發dfs, 求出&#xff1a; l[u] : u 到 1 的最短路徑過中和鏈的交點&#xf…

C#結構類型圖

轉載于:https://www.cnblogs.com/kangao/p/8674838.html

C# 全局鉤子實現掃碼槍獲取信息

1.掃描槍獲取數據原理基本相當于鍵盤數據&#xff0c;獲取掃描槍掃描出來的數據&#xff0c;一般分為兩種實現方式。 a&#xff09;文本框輸入獲取焦點&#xff0c;掃描后自動顯示在文本框內。 b&#xff09;使用鍵盤鉤子&#xff0c;勾取掃描槍虛擬按鍵&#xff0c;根據按鍵頻…

Centos下安裝mysql(二進制版)

Centos下安裝mysql&#xff08;二進制版&#xff09; 1.下載安裝包&#xff0c;選擇相應的平臺、版本&#xff0c;比如&#xff0c;選擇64位Linux平臺下的MySQL二進制包“Linux-Generic &#xff08;glibc 2.5&#xff09;&#xff08;x86&#xff0c;64-bit&#xff09;&#…

使用gradle多渠道打包

以友盟的多渠道打包為例&#xff0c;如果我們須要打包出例如以下渠道&#xff1a;UMENG, WANDOUJIA, YINGYONGBAO。 第一種方法。是須要創建文件的。我們在寫完我們的代碼之后&#xff0c;在app/src以下。分別創建和main同級目錄的目錄umeng, wandoujia, yingyongbao,這三個目錄…

SMMS 2016 啟用深色主題

1、用文本類編輯器 打開C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio目錄下的 ssms.pkgundef 2、去除// Remove Dark theme行以下的注釋 3、重新打開SMMS&#xff0c;如果還沒有出現“深色”主題&#xff0c;請執行第4點 4、打開powershell【…

四大步驟,徹底關閉Win10自動更新

盡管Win11已經發布了一段時間&#xff0c;但目前互聯網上大部分電腦用戶所使用的的操作系統仍是Win10&#xff0c;對于Win10&#xff0c;筆者相信大部分人應該都不陌生&#xff0c;作為目前市面上占比最高的電腦系統&#xff0c;Win10的許多功能和操作邏輯都十分優秀&#xff0…

LeetCode算法題-Repeated String Match(Java實現)

這是悅樂書的第289次更新&#xff0c;第307篇原創 01 看題和準備 今天介紹的是LeetCode算法題中Easy級別的第156題&#xff08;順位題號是686&#xff09;。給定兩個字符串A和B&#xff0c;找到A必須重復的最小次數&#xff0c;使得B是它的子字符串。 如果沒有這樣的解決方案&a…

php

●轉載于:https://www.cnblogs.com/volcanorao/p/8678104.html

Vs快捷鍵設置(可搭配Vim使用)

設置方式: 通過在Vs菜單欄的工具->選項->環境->鍵盤。 常用快捷鍵: 推薦鍵位編輯.轉到定義Alt G切換標題代碼文件Alt Q查看.向前導航Alt D查看.向后導航Alt A調試.調用堆棧Alt 7調試.監視1Alt 8調試.內存1Alt 9查看.查找符號結果Alt 1查看.錯誤列表Alt …