Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Analysis Services
Android
Architecture
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
Bing
BizTalk
Book Excerpts
Build and Deploy
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
Lightswitch
LINQ
Linux
Mac OS X
MDX
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
Network
NHibernate
node.js
NOSQL
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Search
Security
Services
SharePoint
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
Tips
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Server
Windows Vista
WinForms
Workflow
WPF
XAML
XML
XNA
XSLT



State of .NET


TOWER 48
 


INETA

Reader rating:
Article source: CoDe (2012 Jan/Feb)


Article Pages:  1  2 3 - Next >


Working with Windows Phone User Interfaces, Part 1

Developing for Windows Phone is easy if you have been doing any XAML at all. That’s because you use Silverlight for Windows Phone development.

This is a great thing because everything you already know can be applied immediately. This is not to say that there are no new things that you will have to learn. In fact, with the reduced screen size, the ability to change the orientation of the phone and some of the new types of pages, there are actually quite a few things you will need to learn. This article and the next one is your introduction to some of these new UI techniques.

This article assumes that you have Visual Studio 2010 and the Windows Phone tools installed along with it. The Windows Phone tools must be downloaded separately and installed with VS 2010. You may also download the free Visual Studio 2010 Express for Windows Phone developer environment.

Working with the List Box Control

To start out, you will first build a list box that displays a list of products as shown in Figure 1. This is just a standard Windows Phone Page onto which you drag a ListBox control and bind the ItemsSource property of the list box to a collection class of Product objects. You then create a DataTemplate for each ListBoxItem to display the product name and price.

Click for a larger version of this image.

Figure 1: Windows Phone list box with images.

Build the Product Class

In order to get the list box populated with products, you will need two classes: a Product class with three properties, and a collection class of Product objects. The Product class contains the ProductName, Price and ImageUri properties. For this first sample you will not use the ImageUri, but that property will be used in the next sample. Create the Product class as shown in the following code:

public class Product {
  public Product() { }

  public Product(string name, decimal price,
                 string imageUri)
  {
    this.ProductName = name;
    this.Price = price;
    this.ImageUri = imageUri;
  }

  public string ProductName { getset; }
  public decimal Price { getset; }
  public string ImageUri { getset; }
}

The Products class contains an ObservableCollection of Product objects in a property named DataCollection. The constructor of the Products class calls a method named BuildCollection to populate the DataCollection with some sample data as shown in Listing 1.

Value Converter

As you can see in Figure 1, Price is shown in a currency format. Unfortunately, in the current version of Silverlight for Windows Phone, the StringFormat attribute on the Binding class is absent. Therefore, you need to create a value converter for displaying a value in currency format. Create the PriceConverter class shown in Listing 2 that implements the IValueConverter interface. In the Convert method, you take the “value” parameter passed in and convert it to a decimal value. You then return that value as a string formatted with the “c” option in the ToString() method.

Create Classes in XAML

Now that you have the Product class, the Products class and the PriceConverter classes created, you need to create an instance of the Products collection class and the PriceConverter class in your XAML. In your MainPage.xaml, add an xml namespace to the name of your project as shown here:

xmlns:data="clr-namespace:WPListBox"

Next, you create an instance of the Products and PriceConverter class in XAML. The constructor for the Products class will create the initial collection of product objects and load the DataCollection property.

<phone:PhoneApplicationPage.Resources>
  <data:Products x:Key="productCollection" />
  <data:PriceConverter x:Key="priceConvert" />
</phone:PhoneApplicationPage.Resources>

These two classes may now be used within the rest of your XAML by referencing them by their x:Key name.

Create List Box

Now it is time to create the List Box and connect it to the static resource you created in the XAML above. Listing 3 shows the XAML you use to build the list box for this sample.

Let’s break down each of the areas of the ListBox XAML shown in Listing 3. The ItemsSource property references the static resource you created named “productCollection”. This is an instance of the Products class. The Path attribute refers to the DataCollection property that was populated by the BuildCollection method that is called in the constructor of the Products class.

Next, you have the definition for each row in the list box defined in the ItemsTemplate. The ItemsTemplate should not be too foreign to anyone that has done XAML. Each row of the ListBox is defined within the <DataTemplate> area of the ItemsTemplate. The DataTemplate contains a <StackPanel> control with two TextBlock controls within it. This allows the two TextBlock controls to be displayed one on top of the other.

Notice in the text block that displays the Price the use of the value converter class you created earlier. You bind to the Price property using the Path attribute of the Binding class. However, before the Binding class displays the Price, it passes the value to the converter. The Convert() method of the PriceConverter class formats the price and returns it as a string to be displayed in the TextBlock.

&

By: Paul Sheriff

Paul D. Sheriff is the President of PDSA, Inc. (www.pdsa.com), a Microsoft Partner in Southern California. Paul acts as the Microsoft Regional Director for Southern California assisting the local Microsoft offices with several of their events each year and being an evangalist for them. Paul has authored several books, webcasts, videos and articles on .NET, SQL Server and SharePoint. Paul can be reached via email at PSheriff@pdsa.com or at Paul Sheriff's Inner Circle (www.PaulSheriffInnerCircle.com).

PSheriff@pdsa.com



Listing 1: The Products collection class will build a collection of Product objects
public class Products
{
  public Products()
  {
    BuildCollection();
  }

  private const string IMG_PATH = "Images/";

  public ObservableCollection<Product> DataCollection { getset; }

  public ObservableCollection<Product> BuildCollection()
  {
    DataCollection = new ObservableCollection<Product>();

    DataCollection.Add(new Product(
        "Haystack Code Generator for .NET", 799, 
        IMG_PATH + "Haystack.jpg"));
    DataCollection.Add(new Product(
        "PDSA .NET Productivity Framework",
        Convert.ToDecimal(2500), IMG_PATH + "framework.jpg"));

    // MORE DATA HERE

    return DataCollection;
  }
}


Listing 2: A Value Converter class is needed for formatting currency values
public class PriceConverter : IValueConverter
{
  public object Convert(object value, Type targetType, 
     object parameter, System.Globalization.CultureInfo culture)
  {
    decimal price;

    price = (decimal)value;

    return price.ToString("c");
  }

  public object ConvertBack(object value, Type targetType, 
    object parameter, System.Globalization.CultureInfo culture)
  {
    throw new NotImplementedException();
  }
}


Listing 3: Use a DataTemplate to build the ListBox
<ListBox x:Name="lstData"
     ItemsSource="{Binding 
                    Source={StaticResource productCollection}, 
                    Path=DataCollection}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <StackPanel>
          <TextBlock Margin="8" Width="250"
                      TextWrapping="Wrap"
                      VerticalAlignment="Top"
                      HorizontalAlignment="Left"
                      Text="{Binding Path=ProductName}" />
          <TextBlock Width="100"
                      Margin="8,0,8,8"
                      VerticalAlignment="Top"
                      HorizontalAlignment="Left"
                      Text="{Binding Path=Price,
                      Converter={StaticResource priceConvert}}" />
        </StackPanel>
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>


Article Pages:  1  2 3 - Next Page: 'Change the Template when the Orientation Changes' >>

Page 1: Working with Windows Phone User Interfaces, Part 1
Page 2: Change the Template when the Orientation Changes
Page 3: Panorama Control

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
4 out of 5

1 people have rated this article.

TOWER 48

      Android DevCon

 

Sharepoint TechCon