Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Windows Store Apps > Windows Store and Page (C#) > Windows Store Apps and Layout การจัดการ Grid Layout บน XAML (C#)



Clound SSD Virtual Server

Windows Store Apps and Layout การจัดการ Grid Layout บน XAML (C#)

Windows Store Apps and Layout การจัดการ Grid Layout บน XAML (C#) ในการเขียน App โปรแกรมบน Windows Store Apps สิ่งที่เราจะได้เรียนรู้และพบเห็นในอันดับแรก ๆ คือ การจัดวาง Layout บนหน้าจอ UI Design หรือตำแหน่งต่าง ๆ ของ Control บนหน้าจอของ Page และใน Windows Store ด้วยภาษา C# จะใช้ XAML เป็นมาตฐานที่จะรองรับการจัดวางของ Layout ได้หลายรูปแบบ แต่รูปแบบที่ได้รับความนิยมในการจัดส่ง Controls ให้เป็นระเบียบเรียบร้อย คือ Grid Layout

Windows Store Apps and Layout


โดยจะมีการใช้การจัดตำแหน่งต่าง ๆ บนหน้า Page ด้วย Grid Column ซึ่งจะ Grid จะแบ่ง Page ออกเป็นส่วน ๆ ในรูปแบบของ Rows และ Columns (เหมือน Table บน HTML) และจากที่เราได้ Row และ Column แล้ว เราจะจัดวางตำแหน่งของ Control ในช่องของ Grid ตามช่องต่าง ๆ ที่ได้แบ่งออกเป็น Rows และ Column ยกตัวอย่างเช่น

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0">
            <TextBlock x:Name="ApplicationTitle" Text="Welcome to ThaiCreate.Com" FontSize="50"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="410*"/>
                <ColumnDefinition Width="273*"/>
            </Grid.ColumnDefinitions>
            <Button Content="Welcome to Windows Store Tutorial" Name="button" FontSize="50" Grid.ColumnSpan="2" />
        </Grid>

    </Grid>

คำอธิบาย
		<Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

Grid แบ่งหน้าจอของ Page ออกเป็น 2 Rows โดย Row จะมีการเริ่มนับจากจตำแหน่งที่ 0,1,2... โดยจะใช้ RowDefinitions (จัดการ Row)

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0">
            <TextBlock x:Name="ApplicationTitle" Text="Welcome to ThaiCreate.Com" FontSize="50"/>
        </StackPanel>

วาง Control ชุดนี้ในตำแหน่ง Row ที่ 1

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="410*"/>
                <ColumnDefinition Width="273*"/>
            </Grid.ColumnDefinitions>
            <Button Content="Welcome to Windows Store Tutorial" Name="button" FontSize="50" Grid.ColumnSpan="2" />
        </Grid>

วาง Control ชุดนี้ในตำแหน่ง Row ที่ 2

และจาก Code เราจะได้หน้าจอ Design บน Page ดังนี้

Windows Store Apps and Layout

ซึ่งจะเห็นว่าถูกแบ่ง Layout เป็น 2 Rows








Example 1 การใช้ Grid Layout แบ่ง RowDefinitions และ ColumnDefinitions ในตัวอย่างนี้จะลองใช้ Grid โดยแบ่ง Row และ Column จากนั้นแสดงผลตำแหน่งของ Control ในช่องต่าง ๆ โดยจะยกตัวอย่างการแบ่งเป็น 4 แถว 3 Column

Windows Store Apps and Layout

การแบ่ง 4 แถว 3 คอลัมบ์
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>


Code เต็มๆ
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        
		<!--Create a 4 x 3 grid to store an title image and button layout.-->
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <!-- Rows 1 -->
        <Button Content="Button 01" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" Name="Button01" VerticalAlignment="Center" />
        <Button Content="Button 02" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" Name="Button02" VerticalAlignment="Center" />
        <Button Content="Button 03" Grid.Row="0" Grid.Column="3" HorizontalAlignment="Center" Name="Button03" VerticalAlignment="Center" />

        <!-- Rows 2 -->
        <Button Content="Button 04" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" Name="Button04" VerticalAlignment="Center" />
        <Button Content="Button 05" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" Name="Button05" VerticalAlignment="Center" />
        <Button Content="Button 06" Grid.Row="1" Grid.Column="3" HorizontalAlignment="Center" Name="Button06" VerticalAlignment="Center" />

        <!-- Rows 3 -->
        <Button Content="Button 07" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Center" Name="Button07" VerticalAlignment="Center" />
        <Button Content="Button 08" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Center" Name="Button08" VerticalAlignment="Center" />
        <Button Content="Button 09" Grid.Row="2" Grid.Column="3" HorizontalAlignment="Center" Name="Button09" VerticalAlignment="Center" />

        <!-- Rows 4 -->
        <Button Content="Button 10" Grid.Row="3" Grid.Column="0" HorizontalAlignment="Center" Name="Button10" VerticalAlignment="Center" />
        <Button Content="Button 11" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Center" Name="Button11" VerticalAlignment="Center" />
        <Button Content="Button 12" Grid.Row="3" Grid.Column="3" HorizontalAlignment="Center" Name="Button12" VerticalAlignment="Center" />

    </Grid>

จาก Code จะเห็นว่าจะมีการจัดสางตำแหน่งของ Control ใน Row และ Column ในตำแหน่งต่าง ๆ เช่น

Grid.Row="0" Grid.Column="0"

วางตำแถวที่ 1 และคอลัมบ์ที่ 1 (เริ่มนักจาก 0)

<Button Content="Button 01" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" Name="Button01" VerticalAlignment="Center" />

วาง Button ใน Row ที่ 0 และ Column ที่ 0

มุมมอง Design บน UI

Windows Store Apps and Layout

Screenshot

Windows Store Apps and Layout



Example 2 การใช้ RowSpan และ ColumnSpan เพื่อ Merge Cell ของ Grid
ในกรณีที่ต้องการ Marge Cell ของ Grid ใน Row และ Column ต่าง ๆ ก็สามารถทำได้เช่นเดียวกัน โดยการใช้คำสั่ง RowSpan (Marge Cell ที่เป็น Row) และ ColumnSpan (Marge Cell ที่เป็น Column)

Windows Store Apps and Layout

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <!--Create a 4 x 3 grid to store an title image and button layout.-->
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <!-- Rows 1 -->
        <Button Content="Button 01" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" HorizontalAlignment="Center" Name="Button01" VerticalAlignment="Center" />
        <Button Content="Button 02" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" Name="Button02" VerticalAlignment="Center" />
        <Button Content="Button 03" Grid.Row="0" Grid.Column="3" HorizontalAlignment="Center" Name="Button03" VerticalAlignment="Center" />

        <!-- Rows 2 -->
        <StackPanel Background="Transparent" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2">
            <TextBlock Height="30" Name="TextBlock1" Text="TextBlock 1" HorizontalAlignment="Center" />
            <TextBlock Height="30" Name="TextBlock2" Text="TextBlock 2" HorizontalAlignment="Center" />
            <TextBlock Height="30" Name="TextBlock3" Text="TextBlock 3" HorizontalAlignment="Center" />
            <TextBlock Height="30" Name="TextBlock4" Text="TextBlock 4" HorizontalAlignment="Center" />
            <TextBlock Height="30" Name="TextBlock5" Text="TextBlock 5" HorizontalAlignment="Center" />
            <TextBlock Height="30" Name="TextBlock6" Text="TextBlock 6" HorizontalAlignment="Center" />
        </StackPanel>

        <!-- Rows 3 -->
        <Button Content="Button 05" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Center" Name="Button05" VerticalAlignment="Center" />
        <Button Content="Button 06" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Center" Name="Button06" VerticalAlignment="Center" />
        <Button Content="Button 07" Grid.Row="2" Grid.Column="3" HorizontalAlignment="Center" Name="Button07" VerticalAlignment="Center" />

        <!-- Rows 4 -->
        <Button Content="Button 08" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Center" Name="Button10" VerticalAlignment="Center" />
        
    </Grid>


Screenshot

Windows Store Apps and Layout







.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2014-01-19 12:56:19 / 2017-03-19 14:34:39
  Download : No files
 Sponsored Links / Related

 
การปรับแต่ง Simulator การกำหนด Mode ของ Landscape , Portrait (แนวนอน-ตั้ง)
Rating :

 
Page และ Controls สำหรับการออก Windows Store Apps ด้วย XAML UI (C#)
Rating :

 
Event Handler บน Windows Store Apps กับ Controls ผ่าน XAML (C#)
Rating :

 
MessageBox Dialog / Confirm Dialog บน Windows Store Apps (C#)
Rating :

 
สร้าง Pages มากกว่า 1 Page และ Navigation บน Windows Store Apps (C#)
Rating :

 
Windows Store Apps การส่ง ตัวแปร หรือ Parameters ข้าม Page (C#)
Rating :

 
รู้จักกับ Resources และ Styling บน XAML กับ Windows Store Apps (C#)
Rating :

 
การสร้าง Resources และ Custom Style กับ ResourceDictionary (C#)
Rating :

 
การเรียกใช้งาน Image Resource ไฟล์ที่อยู่บน Local file หรือจาก URL เว็บ (C#)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่