DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Laying Out Windows Phone ListBoxes in 4 Steps

Laying Out Windows Phone ListBoxes in 4 Steps

Tim Murphy user avatar by
Tim Murphy
·
Feb. 01, 12 · Mobile Zone · Interview
Like (0)
Save
Tweet
3.43K Views

Join the DZone community and get the full member experience.

Join For Free

A listbox can be a very boring display surface, but it doesn’t have to be.  Ok, so it may never be exciting.  At least we can create a more flexible output.

The first element you need to learn about his the ItemTemplate which is in turn composed of a DataTemplate. As this combo implies it is bound to each item/data row.  We will start organizing your base layout with a Grid control.

Within the Grid you can add a RowDefintions group.  A RowDefinition helps when you want components to be stacked for a particular data row.  The main attribute for a RowDefinition is it’s Height.  There is no name attribute, but we will get to that point toward the end of this article.

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


The other main layout organization is kept in the ColumnDefinitions group.  ColumnDefinition elements are best used for defining the width. I only created column definitions where they differ from full width of the row.  In the case of this example the first of the two rows.

<Grid.ColumnDefinitions>
     <ColumnDefinition Width="300"/>
     <ColumnDefinition Width="90"/>
 </Grid.ColumnDefinitions>


Now that you have all of definition setup lets put it to use.  In this example we have a project list.  Each project has a name, a score and a description.  We will add a group of TextBlock controls for each data element.  They do not need to be in any particular order and I will show why next.

Now we can assign row and column associations to each TextBlock. Both rows and columns are assigned using a zero based index from the sequence they are defined in the XMAL.  At this point the layout is set.

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


The last thing we need to do is Databind the components of the list.  Set the Text attribute of each TextBlock to an appropriate field of the object your are binding to each object.

Text="{Binding ProjectName}"


The final code is listed below.

<ListBox Height="330" HorizontalAlignment="Left" Margin="32,20,0,0" Name="listEvaluations" VerticalAlignment="Top" Width="397" ItemsSource="{Binding}" MouseEnter="listEvaluations_MouseEnter"> 
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="300"/>
                    <ColumnDefinition Width="90"/>
                </Grid.ColumnDefinitions>
                <TextBlock HorizontalAlignment="Left" Grid.Row="0" Grid.Column="0" Name="ProjectName" Text="{Binding ProjectName}" FontSize="{StaticResource PhoneFontSizeLarge}"></TextBlock>
                <TextBlock HorizontalAlignment="Right" Grid.Row="0" Grid.Column="1" Name="ProjectTotal" Text="{Binding Total}" FontSize="{StaticResource PhoneFontSizeLarge}"></TextBlock>
                <TextBlock Name="ProjectDescription" Grid.Row="1" Text="{Binding Description}" FontSize="{StaticResource PhoneFontSizeSmall}"></TextBlock>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>


And here is what the result looks like.



There is a lot more you can do with this approach by using other controls in your template, but this should give you the basic concepts you need.



Source: http://geekswithblogs.net/tmurphy/archive/2011/09/02/laying-out-windows-phone-7-listboxes.aspx

Windows Phone

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java’s Encapsulation - When the Getter and Setter Became Your Enemy
  • When Writing Code Isn't Enough: Citizen Development and the Developer Experience
  • Is DataOps the Future of the Modern Data Stack?
  • MACH Architecture Explained

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo