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 > Mobile > Windows Phone Dev - สอนเขียน App บนโปรแกรม Windows Phone 7 , Windows Phone 8 > Windows Phone and Grid จัดวาง Layout ด้วย RowDefinition และ ColumnDefinition



Clound SSD Virtual Server

Windows Phone and Grid จัดวาง Layout ด้วย RowDefinition และ ColumnDefinition

Windows Phone and Grid จัดวาง Layout ด้วย RowDefinition และ ColumnDefinition ในการเขียน App โปรแกรมบน Windows Phone สิ่งที่เราจะได้เรียนรู้และพบเห็นในอันดับแรก ๆ คือ การจัดวาง Layout หรือตำแหน่งต่าง ๆ ของ Control บนหน้าจอของ Page และใน Windows Phone จะใช้ XAML เป็นมาตฐานที่จะรองรับการจัดวางของ Layout โดยจะมีการใช้การจัดตำแหน่งต่าง ๆ บนหน้า Page ด้วย Grid ซึ่งจะ Grid จะแบ่ง Page ออกเป็นส่วน ๆ ในรูปแบบของ Rows และ Columns (เหมือน Table บน HTML) และจากนั้นเราจะจัดวางตำแหน่งของ Control ในช่องของ Grid ตามช่องต่าง ๆ ที่ได้แบ่งออกเป็น Rows และ Column ยกตัวอย่างเช่น

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <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="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1">
            <TextBlock Height="62" HorizontalAlignment="Left" Name="lblTitle" Text="i = " VerticalAlignment="Top" TextAlignment="Center" FontSize="32" Width="82" />
            <TextBlock FontSize="32" Height="62" HorizontalAlignment="Left" Name="lbli" Text="0" TextAlignment="Center" VerticalAlignment="Top" Width="82" />
            <Button Content="i = i + 1" Height="72" HorizontalAlignment="Left" Name="btn1" VerticalAlignment="Top" Width="264" />
            <Button Content="i = i + 2" Height="72" HorizontalAlignment="Left" Name="btn2" VerticalAlignment="Top" Width="264" />
        </Grid>
    </Grid>

คำอธิบาย

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
Grid แบ่งหน้าจอของ Page ออกเป็น 2 Rows โดย Row จะมีการเริ่มนับจากจตำแหน่งที่ 0,1,2...

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0">
        <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock x:Name="PageTitle" Text="page name" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>
วาง Control ชุดนี้ในตำแหน่ง Row ที่ 1

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" >
        <TextBlock Height="62" HorizontalAlignment="Left" Name="lblTitle" Text="i = " VerticalAlignment="Top" TextAlignment="Center" FontSize="32" Width="82" />
        <TextBlock FontSize="32" Height="62" HorizontalAlignment="Left" Name="lbli" Text="0" TextAlignment="Center" VerticalAlignment="Top" Width="82" />
        <Button Content="i = i + 1" Height="72" HorizontalAlignment="Left" Name="btn1" VerticalAlignment="Top" Width="264" />
        <Button Content="i = i + 2" Height="72" HorizontalAlignment="Left" Name="btn2" VerticalAlignment="Top" Width="264" />
    </Grid>
วาง Control ชุดนี้ในตำแหน่ง Row ที่ 2




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

Windows Phone and Grid 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 เต็มๆ
    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent" ShowGridLines="True">
        
        <!--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)

มุมมอง Design บน UI

Windows Phone and Grid Layout

Screenshot

Windows Phone and Grid Layout

ผลลัพธ์ที่ได้เมื่อรันผ่าน Emulator จะเห็นว่ามีการแสดงผลในตำแหน่งต่าง ๆ ของ Grid









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

Windows Phone and Grid Layout


    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent" ShowGridLines="True">
        
        <!--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 Phone and Grid Layout

ผลลัพธ์ที่ได้เมื่อรันผ่าน Emulator

เพิ่มเติม

Windows Phone and Grid Layout

จากตัวอย่างจะมีการใช้ ShowGridLines="True" เพื่อแสดงเส้นของ Grid และถ้าไม่ต้องการให้กำหนดเป็น False หรือลบ Attribute นี้ทิ้งไป

Windows Phone and Grid Layout

เมื่อกำหนด ShowGridLines="False" หรือลบออกไป

จากทั้ง 2 ตัวอย่างจะเห็นว่าการจัดวาง Layout บน Windows Phone ที่เป็น Silverlight จะใช้หลักการเหมือนกับการวางตำแหน่ง HTML Table ในการเขียนเว็บ เพราะฉะนั้นถ้าเคยเขียนเว็บเป็นประจำอยู่แล้วก็สามารถที่จะเข้าได้ในทันที







.

   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-08-20 20:40:17 / 2017-03-25 21:45:23
  Download : Download  Windows Phone and Grid จัดวาง Layout ด้วย RowDefinition และ ColumnDefinition
 Sponsored Links / Related

 
Orientations Landscape มุมมอง Layout ในแนวตั้งและแนวนอนบน Windows Phone
Rating :

 
Margin Property และการจัดวาง Layout ในหน้า Silverlight Page, Windows Phone
Rating :

 
การสร้าง Page และการ Link แสดงและซ่อน Uri Page Navigation บน Windows Phone
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 02
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 อัตราราคา คลิกที่นี่