Archives / 2010
  • Using Xml Files on Windows Phone

    The Windows Phone does not have a database for you to use locally on the phone. This means that you will need to use WCF services to get data from a database. However, each time you make a call to a WCF service this means that you need to connect to the network, make a call to a remote service, get the data back and put it into a format that you can consume on the phone. All of this takes time, and just as important, consumes battery power. If you have some data that is fairly static you could deploy that data with your application in the form of an XML file. With the XML file deployed on the phone as a part of your application, you use LINQ to XML to read the data. That data can now be displayed in any format you choose.

  • Using Isolated Storage on Windows Phone

    On a Windows Phone application, or actually in any application, you will sometimes want to keep information around from one session to another. On a Windows Phone it is sometimes especially important as it is a little difficult to type on these small devices. If you need the user to login, fill in their name and email, or enter other information each time they use the application, then you might want to store that data on the phone and retrieve it each time they come back in to the application. Isolated storage is the place where you are can store this information for your application.

  • Change Templates When Orientation Changes on Windows Phone

    Developing for Windows Phone does require you to think a little differently. For example on a regular computer you only have to worry about the screen orientation being in portrait mode. However, on a phone the user can turn the phone sideways and even upside down. If the user switches from portrait orientation (Figure 1) to landscape orientation (Figure 2) you end up with more room to display your data horizontally. You can take advantage of this extra room by switching between two different XAML templates when the phone orientation changes.

  • Windows Phone List Box with Images

    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 you can apply immediately. Let’s take a simple example like presenting a list of products with images in a list box (Figure 1) in the Windows Phone emulator. This article assumes that you have VS.NET 2010 and the Windows Phone tools installed along with it. The Windows Phone tools must be downloaded separately and installed with VS.NET 2010. You may also download the free VS.NET 2010 Express for Windows Phone developer environment.

  • Horizontal List Boxes in Silverlight

    A list box in XAML does not just have to look like a normal list box with just a single line of data in each row. You can place multiple rows into the list box, and you can even turn the list box so it displays the data horizontally instead of vertically (see Figure 1).

  • Using Value Converters in Silverlight

    When you start to bind data in Silverlight (or WPF for that matter) you will most likely have data that is in one format that you may need to convert to another format. For example if you have a decimal number (price) that is 19.95, you would want to display it as $19.95. Of course you can use a normal .NET format string to accomplish this, but you could also write some code to convert the decimal number 19.95 to a string with the dollar sign in front of it “$19.95”.

  • Change Templates Dynamically in Silverlight

    Silverlight has the flexibility to modify the look of your controls dynamically at runtime with just a few lines of code. Take a look at Figures 1 and 2 and you will see two different views of the same list box and data. To accomplish this, you simply need to setup two different resources of XAML code that you can switch between at runtime.

  • Filtering CollectionView Data in Silverlight 4

    If you are using Silverlight's ListBox control, you can filter the data in a CollectionViewSource object using just a little bit of simple code. For this example, I will be using a simple Product class with two properties, and a list of Product objects using the Generic List class. Try this out by creating a Product class as shown in the following code:

  • Sort Data Using Code with the Silverlight CollectionViewSource

    As a follow-up to last week's blog posting and newsletter, this week I am going to show you how to use the CollectionViewSource object in Silverlight to sort data using code. Sometimes you need something a little more flexible, so you will need to resort to writing some code. You can take advantage of the CollectionViewSource from your XAML, but dynamically change the sort order of your lists with just a few lines of code.

  • Use CollectionViewSource in Silverlight to Sort Data

    If you are using Silverlight's ListBox control, you do not have to write code to sort your data. Instead you can use the built-in CollectionViewSource object in XAML to perform the sorting for you. This assumes that you are using a collection that implements the IEnumerable or IList interfaces.

  • WPF ListView as a DataGrid – Part 3

    I have had a lot of great feedback on the blog post about turning the ListView into a DataGrid by creating GridViewColumn objects on the fly. So, in the last 2 parts, I showed a couple of different methods for accomplishing this. Let’s now look at one more and that is use Reflection to extract the properties from a Product, Customer, or Employee object to create the columns. Yes, Reflection is a slower approach, but you could create the columns one time then cache the View object for re-use. Another potential drawback is you may have columns in your object that you do not wish to display on your ListView. But, just because so many people asked, here is how to accomplish this using Reflection.

  • Control to Control Binding in WPF/Silverlight

    In the past if you had two controls that you needed to work together, you would have to write code. For example, if you want a label control to display any text a user typed into a text box you would write code to do that. If you want turn off a set of controls when a user checks a check box, you would also have to write code. However, with XAML, these operations become very easy to do.

  • Synchronize Data between a Silverlight ListBox and a User Control

    One of the great things about XAML is the powerful data-binding capabilities. If you load up a list box with a collection of objects, you can display detail data about each object without writing any C# or VB.NET code. Take a look at Figure 1 that shows a collection of Product objects in a list box. When you click on a list box you bind the current Product object selected in the list box to a set of controls in a user control with just a very simple Binding statement in XAML.

  • WPF ListView as a DataGrid – Part 2

    In my last blog post I showed you how to create GridViewColumn objects on the fly from the meta-data in a DataTable. By doing this you can create columns for a ListView at runtime instead of having to pre-define each ListView for each different DataTable. Well, many of us use collections of our classes and it would be nice to be able to do the same thing for our collection classes as well. This blog post will show you one approach for using collection classes as the source of the data for your ListView.

  • Using a WPF ListView as a DataGrid

    Many people like to view data in a grid format of rows and columns. WPF did not come with a data grid control that automatically creates rows and columns for you based on the object you pass it. However, the WPF Toolkit can be downloaded from that does contain a DataGrid control. This DataGrid gives you the ability to pass it a DataTable or a Collection class and it will automatically figure out the columns or properties and create all the columns for you and display the data.
    The DataGrid control also supports editing and many other features that you might not always need. This means that the DataGrid does take a little more time to render the data. If you want to just display data (see Figure 1) in a grid format, then a ListView works quite well for this task. Of course, you will need to create the columns for the ListView, but with just a little generic code, you can create the columns on the fly just like the WPF Toolkit’s DataGrid.

  • Bind ObjectDataProvider Method Parameters in WPF

    It is absolutely amazing how much you can do in WPF without writing a line of code. If you take full advantage of the data-binding features in WPF you will find that a lot of the code you used to write to move data from one control to another, or move data from a control on a form and pass it to the method on a class can be completely eliminated!

  • Use XML Files with WPF

    XML files are very handy for storing and display data that you might not want to keep in a database. It is wonderful choice for prototyping since you do not need to setup any database tables, connection strings and data objects. Instead you can send a complete project to a user that they can try out immediately without any setup at all. There are actually several methods you can use to display XML within a WPF application, let’s look at a few.

  • Set the MinWidth and MinHeight based on SizeToContent Property

    We are developing a WPF application for a client where they want to be able to use the SizeToContent=”WidthAndHeight” attribute on the window. This attribute is great because it allows WPF to size the window to the size of the controls on the window. This is especially important as they need the labels to grow and shrink when they localize the labels for English, Spanish and German. Once the initial size is set by WPF, they want that initial size to be the minimum height and minimum width for that window. As it turns out, this is very easy to accomplish in WPF.

  • Centering Text within a WPF Shape using a Canvas

    In my last blog post, I showed you how to use a Shape control, a Border control and a TextBlock to create rectangles, circles, ellipsis, and triangles with text in the middle of the shape. You also learned how to use a VisualBrush in a TextBlock to help you center the text as well. In this post, you will learn another technique for accomplishing the same task. This blog will show you how to use a Canvas, a Shape and a TextBlock. The reason for using a Canvas is to introduce you to using a Multi-Binding converter in WPF.

Past Blog Content

Blog Archive