背水一戰 Windows 10 (38) - 控制項(佈局類): Panel, Canvas, RelativePanel, StackPanel, Grid

来源:http://www.cnblogs.com/webabcd/archive/2017/01/10/6269510.html
-Advertisement-
Play Games

背水一戰 Windows 10 之 控制項(佈局類): Panel, Canvas, RelativePanel, StackPanel, Grid ...


[源碼下載]


背水一戰 Windows 10 (38) - 控制項(佈局類): Panel, Canvas, RelativePanel, StackPanel, Grid



作者:webabcd


介紹
背水一戰 Windows 10 之 控制項(佈局類)

  • Panel
  • Canvas
  • RelativePanel
  • StackPanel
  • Grid



示例
1、Panel(基類) 的示例
Controls/LayoutControl/PanelDemo.xaml

<Page
    x:Class="Windows10.Controls.LayoutControl.PanelDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.LayoutControl"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">

            <!--
                StackPanel - 流式佈局控制項,繼承自 Panel,下麵介紹 Panel 的相關知識點
                    Children - 子元素集合([ContentProperty(Name = "Children")])
                    Background - 背景色
                    ChildrenTransitions - 過渡效果集合
            -->

            <StackPanel Margin="5" Background="Orange">
                <StackPanel.Children>
                    <TextBlock Text="A Panel contains a collection of UIElement objects, which are in the Children property" Margin="5" />
                    <TextBlock Text="Panel elements do not receive focus by default" Margin="5" />
                    <TextBlock Text="To compel a panel element to receive focus, set the Focusable property to true" Margin="5" />
                </StackPanel.Children>
                <StackPanel.ChildrenTransitions>
                    <TransitionCollection>
                        <EntranceThemeTransition />
                    </TransitionCollection>
                </StackPanel.ChildrenTransitions>
            </StackPanel>

        </StackPanel>
    </Grid>
</Page>

Controls/LayoutControl/PanelDemo.xaml.cs

/*
 * Panel(基類) - 面板控制項基類(繼承自 FrameworkElement, 請參見 /Controls/BaseControl/FrameworkElementDemo.xaml)
 */
 
using Windows.UI.Xaml.Controls;

namespace Windows10.Controls.LayoutControl
{
    public sealed partial class PanelDemo : Page
    {
        public PanelDemo()
        {
            this.InitializeComponent();
        }
    }
}


2、Canvas 的示例
Controls/LayoutControl/CanvasDemo.xaml

<Page
    x:Class="Windows10.Controls.LayoutControl.CanvasDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.LayoutControl"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        
        <!--
            Canvas - 絕對定位佈局控制項(Canvas 的子元素使用絕對定位)
                
            Canvas.Left - 與上一層 Canvas 的 Y軸 間的距離,左上角為原點。僅在 Canvas 的子元素中設置有效
            Canvas.Top - 與上一層 Canvas 的 X軸 間的距離,左上角為原點。僅在 Canvas 的子元素中設置有效
            Canvas.ZIndex - 用於設置控制項的 z-index 值(數值大的在前面)。 不要求必須在 Canvas 內
        
            註:Canvas 不會因為自身的大小而限制子元素的大小
        -->

        <!--
            這裡指定 Canvas.Left 和 Canvas.Top 是沒用的,因為它的父親不是 Canvas
        -->
        <Canvas Margin="10 0 0 0" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Orange" Width="100" Height="100"
                Canvas.Left="100" Canvas.Top="100">

            <Canvas Background="Blue" Width="200" Height="200" Canvas.Left="100" Canvas.Top="100">
                <!--
                    Canvas 不會因為自身的大小而限制子元素的大小 
                -->
                <TextBlock Text="TextBlock TextBlock TextBlock TextBlock TextBlock" />

                <!--
                    Canvas.ZIndex 大的在前面,所以此處黑色會壓著白色
                -->
                <StackPanel Background="Black" Width="50" Height="50" Canvas.Left="50" Canvas.Top="50" Canvas.ZIndex="10" />
                <StackPanel Background="White" Width="50" Height="50" Canvas.Left="75" Canvas.Top="75" Canvas.ZIndex="1" />
            </Canvas>

        </Canvas>
    </Grid>
</Page>

Controls/LayoutControl/CanvasDemo.xaml.cs

/*
 * Canvas - 絕對定位佈局控制項(繼承自 Panel, 請參見 /Controls/LayoutControl/PanelDemo.xaml)
 *     double GetLeft(UIElement element) - 獲取指定 UIElement 的 Canvas.Left 值
 *     double GetTop(UIElement element) - 獲取指定 UIElement 的 Canvas.Top 值
 *     int GetZIndex(UIElement element) - 獲取指定 UIElement 的 Canvas.ZIndex 值
 *     SetLeft(UIElement element, double length) - 設置指定 UIElement 的 Canvas.Left 值
 *     SetTop(UIElement element, double length) - 設置指定 UIElement 的 Canvas.Top 值
 *     SetZIndex(UIElement element, int value) - 設置指定 UIElement 的 Canvas.ZIndex 值
 */

using Windows.UI.Xaml.Controls;

namespace Windows10.Controls.LayoutControl
{
    public sealed partial class CanvasDemo : Page
    {
        public CanvasDemo()
        {
            this.InitializeComponent();
        }
    }
}


3、RelativePanel 的示例
Controls/LayoutControl/RelativePanelDemo.xaml

<Page
    x:Class="Windows10.Controls.LayoutControl.RelativePanelDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.LayoutControl"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <StackPanel Margin="10 0 10 10">

            <!--
                RelativePanel - 相對定位佈局控制項
                    BorderBrush - 邊框畫筆
                    BorderThickness - 邊框寬度(左 上 右 下)
                    CornerRadius - 邊框的角半徑(左上 右上 右下 左下)
                    Padding - 內容與邊框的間距(左 上 右 下)
            
                    LeftOf, RightOf, Above, Below - 在指定元素的左邊, 右邊, 上邊, 下邊
                    AlignLeftWithPanel, AlignRightWithPanel, AlignTopWithPanel, AlignBottomWithPanel - 是否與 RelativePanel 容器的左邊緣, 右邊緣, 上邊緣, 下邊緣對齊
                    AlignLeftWith, AlignRightWith, AlignTopWith, AlignBottomWith - 與指定元素的左邊緣, 右邊緣, 上邊緣, 下邊緣對齊
                    AlignHorizontalCenterWith, AlignVerticalCenterWithPanel - 與指定元素水平居中對齊, 垂直居中對齊
                    AlignHorizontalCenterWithPanel, AlignVerticalCenterWithPanel - 是否相對於 RelativePanel 容器水平居中對齊, 垂直居中對齊
            
                    在 code-behind 中有對應的 Get... 和 Set... 方法來獲取或設置上面這些附加屬性
            -->
            
            <RelativePanel Width="300" Margin="5" 
                           HorizontalAlignment="Left" BorderBrush="Orange" BorderThickness="1" CornerRadius="0" Padding="0">
                
                <Rectangle x:Name="rectangle1" Fill="Red" Height="50" Width="50"/>
                
                <Rectangle x:Name="rectangle2" Fill="Blue" Height="50" Width="50" 
                           RelativePanel.RightOf="rectangle1" />
                
                <Rectangle x:Name="rectangle3" Fill="Green" Height="50" Width="50" 
                           RelativePanel.AlignRightWithPanel="True"/>
                
                <Rectangle x:Name="rectangle4" Fill="Yellow" Height="50" Width="50" 
                           RelativePanel.Below="rectangle3" RelativePanel.AlignHorizontalCenterWith="rectangle3" />

                <!--
                    rectangle5 的上邊緣與 rectangle4 的上邊緣對齊
                -->
                <Rectangle x:Name="rectangle5" Fill="Purple" Height="100" Width="100" 
                           RelativePanel.AlignTopWith="rectangle4" RelativePanel.AlignHorizontalCenterWithPanel="True" />

            </RelativePanel>
            
        </StackPanel>
    </Grid>
</Page>

Controls/LayoutControl/RelativePanelDemo.xaml.cs

/*
 * RelativePanel - 相對定位佈局控制項(繼承自 Panel, 請參見 /Controls/LayoutControl/PanelDemo.xaml)
 */

using Windows.UI.Xaml.Controls;

namespace Windows10.Controls.LayoutControl
{
    public sealed partial class RelativePanelDemo : Page
    {
        public RelativePanelDemo()
        {
            this.InitializeComponent();
        }
    }
}


4、StackPanel 的示例
Controls/LayoutControl/StackPanelDemo.xaml

<Page
    x:Class="Windows10.Controls.LayoutControl.StackPanelDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.LayoutControl"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        <StackPanel HorizontalAlignment="Left" Margin="10 0 10 10">

            <!--
                StackPanel - 流式佈局控制項
                    Orientation - 控制項內元素的排列方向
                        Horizontal - 水平排列
                        Vertical - 垂直排列
                    BorderBrush - 邊框畫筆
                    BorderThickness - 邊框寬度(左 上 右 下)
                    CornerRadius - 邊框的角半徑(左上 右上 右下 左下)
                    Padding - 內容與邊框的間距(左 上 右 下)
            -->

            <StackPanel Background="Orange" Margin="5"
                        Orientation="Vertical" BorderBrush="Red" BorderThickness="1 2 3 4" CornerRadius="10 20 30 40" Padding="10 20 30 40">
                <TextBlock Text="abc" Margin="5" />
                <TextBlock Text="xyz" Margin="5" />
                <TextBlock Text="123" Margin="5" />
            </StackPanel>

        </StackPanel>
    </Grid>
</Page>

Controls/LayoutControl/StackPanelDemo.xaml.cs

/*
 * StackPanel - 流式佈局控制項(繼承自 Panel, 請參見 /Controls/LayoutControl/PanelDemo.xaml)
 */

using Windows.UI.Xaml.Controls;

namespace Windows10.Controls.LayoutControl
{
    public sealed partial class StackPanelDemo : Page
    {
        public StackPanelDemo()
        {
            this.InitializeComponent();
        }
    }
}


5、Grid 的示例
Controls/LayoutControl/GridDemo.xaml

<Page
    x:Class="Windows10.Controls.LayoutControl.GridDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.Controls.LayoutControl"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="Transparent">
        
        <!--
            Grid - 網格佈局控制項
                Grid.RowDefinitions - 用於定義 Grid 中的行
                Grid.ColumnDefinitions - 用於定義 Grid 中的列
                BorderBrush - 邊框畫筆
                BorderThickness - 邊框寬度(左 上 右 下)
                CornerRadius - 邊框的角半徑(左 上 右 下)
                Padding - 內容與邊框的間距(左上 右上 右下 左下)
        
            RowDefinition - 行定義
                Height - 高度
                MinHeight - 最小高度
                MaxHeight - 最大高度
                ActualHeight - 獲取真實高度
        
            ColumnDefinition - 列定義
                Width - 寬度
                MinWidth - 最小寬度
                MaxWidth - 最大寬度
                ActualWidth - 獲取真實寬度
                
            Grid.Row - 控制項所在的 Grid 的行的索引
                code-behind: int GetRow(FrameworkElement element), SetRow(FrameworkElement element, int value)
            Grid.Column - 控制項所在的 Grid 的列的索引
                code-behind: int GetColumn(FrameworkElement element), SetColumn(FrameworkElement element, int value)
            Grid.RowSpan - 合併的行數。 控制項所在行,以及控制項所在行之後的需要連續合併的行的總行數
                code-behind: int GetRowSpan(FrameworkElement element), SetRowSpan(FrameworkElement element, int value)
            Grid.ColumnSpan - 合併的列數。 控制項所在列,以及控制項所在列之後的需要連續合併的列的總列數
                code-behind: int GetColumnSpan(FrameworkElement element), SetColumnSpan(FrameworkElement element, int value)
                        
            Width 和 Height 的可用值如下:
            1、Auto - 自動設置為一個合適的值。預設值
            2、Pixel - 像素值
            3、* - 比例值。如 * 就是全部,2* 和 8* 就是分別占 20% 和 80%
        
            註:Grid 的 HorizontalAlignment 屬性和 VerticalAlignment 屬性的預設值均是 Stretch
        -->
        
        <Grid Background="Blue" Width="300" Height="300" Canvas.ZIndex="10">
            <Grid.RowDefinitions>
                <RowDefinition Height="50" />
                <RowDefinition Height="3*" />
                <RowDefinition Height="7*" />
                <RowDefinition Height="*" MinHeight="50" MaxHeight="100" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <TextBox Grid.Row="0" Grid.Column="0" Background="red" Text="webabcd" />
            <TextBox Grid.Row="0" Grid.Column="1" Background="red" Text="webabcd" Grid.ColumnSpan="2" HorizontalAlignment="Center" />
            <TextBox Grid.Row="1" Grid.Column="0" Background="red" Text="webabcd" />
            <TextBox Grid.Row="1" Grid.Column="1" Background="red" Text="webabcd" Grid.ColumnSpan="2" HorizontalAlignment="Center" />
            <TextBox Grid.Row="2" Grid.Column="0" Background="red" Text="webabcd" />
            <TextBox Grid.Row="2" Grid.Column="1" Background="red" Text="webabcd" Grid.RowSpan="2" VerticalAlignment="Bottom" />
            <TextBox Grid.Row="2" Grid.Column="2" Background="red" Text="webabcd" />
            <TextBox Grid.Row="3" Grid.Column="2" Background="red" Text="webabcd" />
            <TextBox Grid.Row="4" Grid.Column="2" Background="red" Text="webabcd" />
        </Grid>

        <!--    
            Canvas.ZIndex - 用於設置控制項的 z-index 值(不要求必須在 Canvas 內)
        
            註:在 Grid 內的子元素的定位可以通過 Margin 來實現
        -->
        <Rectangle Margin="10 50 100 150" Fill="Green" Canvas.ZIndex="1" />

    </Grid>
</Page>

Controls/LayoutControl/GridDemo.xaml.cs

/*
 * Grid - 網格佈局控制項(繼承自 Panel, 請參見 /Controls/LayoutControl/PanelDemo.xaml)
 */

using Windows.UI.Xaml.Controls;

namespace Windows10.Controls.LayoutControl
{
    public sealed partial class GridDemo : Page
    {
        public GridDemo()
        {
            this.InitializeComponent();
        }
    }
}



OK
[源碼下載]


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在C#.NET的開發中,事件是經常接觸到的概念,比如為按鈕添加點擊事件,並寫入點擊按鈕觸發事件要運行的代碼。不管是ASP.NET還是WinForm等各種形式的應用程式,最經常是為系統生成的事件寫具體代碼。如果要自定義事件呢?有的朋友對於自定義事件感覺比較難理解。最近在開發HoverTreeTop項目 ...
  • C#簡單程式練習 說明:學習之餘溫習幾道經典基礎知識題,將其記錄下來,以供初學者參考。 1,題目:求出0-1000中能被7整除的數,並計算輸出每五個數的和: 運行截圖: 題目2:編寫一個類,其中包含一個排序的方法 Sort(), 當傳入的是一串整數,就按照從小到大的順序輸出,如果傳入的是一個字元串, ...
  • 堆、棧、引用類型、值類型 記憶體分為堆和棧(PS:還有一種是靜態存儲區域 [記憶體分為這三種]),值類型的數據存儲在棧中,引用類型的數據存儲在堆中。 堆、棧: 堆和棧的區別: 棧是編譯期間就分配好的記憶體空間,因此你的代碼中必須就棧的大小有明確的定義;局部值類型變數、值類型參數等都在棧記憶體中。 堆是程式運 ...
  • 本篇和大家分享的是學習Vuejs的總結和調用webapi的一個小示例;快到年底了爭取和大家多分享點東西,希望能對各位有所幫助;本章內容希望大家喜歡,也希望各位多多掃碼支持和推薦謝謝: » Vuejs - 學習大雜燴 » WebApi + Vue.js 示例 下麵一步一個腳印的來分享: » Vuejs ...
  • WEB後臺開發,如果用的是Bootstrap框架,那這個表格神器你一定不要錯過。 官方地址:https://datatables.net/ What?英文不好,沒關係咱有中文的 http://datatables.club/ 不過我還是建議看英文的,因為比較全面雖然訪問的速度慢點,終歸能進的去。閑話 ...
  • 恢復內容開始 http://ironpython.net/documentation/dotnet/這是原文地址 以下筆記僅記錄閱讀過程中我認為有必要記錄的內容,大多數都是依賴翻譯軟體的機翻,配合個人對代碼的理解寫出的筆記,個別不是很確定的,會在句首標註 猜測: 在ironPython 中想使用.N ...
  • 沒時間扯淡類,趕緊上車吧。 在現代社會中,信息安全對於每一個人都是至關重要的,例如我們的銀行賬戶安全、支付寶和微信賬戶安全、以及郵箱等等,說到信息安全,那就必須得提到加密技術,至於加密的一些相關概念,在這裡就不說了。 這一次將會主要講解.NET的加密方式,接下來將會分別介紹散列加密,對稱加密,非對稱 ...
  • 成員的可訪問性 如果沒有顯示聲明成員的可訪問性,編譯器通常預設選擇private。CRL要求介面類型的所有成員都具有public可訪問性。 一個派生類重寫在他的基類型中定義的一個成員時,c#編譯器要求原始成員和重寫成員具有相同的可訪問性。(CRL不需要) 任何成員想要被別人訪問到,都必須在一個可見的 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...