How do I separate out resources into multiple files in WPF?

WPF allows you to define pieces of Xaml as resources, and use them in multiple places in your application. This allows us to share things like styles and brushes across our whole application, making it look more consistent and easier to maintain (since changes need only be made in a single place). As applications grow the size of these resource sections can make them more difficult to work with and create contention amongst the development team as different people try to work on the same large resource file. »

Author image

What do I need to install to get started building WPF applications?

WPF is built on top of the .NET framework 2.0, so the very first thing you will need is Framework 2.0 if you don't already have it. The most recent release of WPF is Beta 2 - released on the 23rd of May 2006. To begin developing WPF applications for Windows Server 2003 or Windows XP SP2 you need to download the Beta 2 release of the WinFx run-time components (WPF is a part of a larger package of new components for windows called WinFx). »

Author image

Pundits Mis-judge WPF/E?

I recently read this analysis of WPF/E by Tim Anderson of itweek. Mr Anderson believes Microsoft has "seen the light" regarding cross platform web applications and that WPF/E offers "further evidence that the future belongs to cross-platform internet clients, not Windows desktops." Non-windows clients account for between 5 and 10 percent of the total desktop market. Windows 3.1 has more desktop users than Linux does. I don't believe WPF/E represents a strategic move away from Windows - I believe it was probably necessary for Microsoft to create WPF/E to support another popular Microsoft franchise - Office. »

Author image

WPF Beta2 Release Imminent?

It’s been almost 3 months since the release of the most recent WPF CTP in late February. It is widely anticipated that a new Vista build, Windows SDK, and accompanying WinFx version will be released next week (week commencing 22-May-2006) to coincide with the WinHEC. The Expression Interactive Designer team has previously indicated they hope to release the next CTP of their application soon after WPF Beta2 hits the streets. We’ve already seen some examples of some of the changes to expect in the next release including the ability to specify a z-Index (like z-order?) for elements, the ability to use Vista glass effects and some small changes to the default project templates. »

Author image

Adam Nathan's Windows Presentation Foundation Unleashed available for pre-order on Amazon

Adam Nathan (of .NET and COM and pinvoke fame) has been working on a WPF book - Windows Presentation Foundation Unleashed, which is now available for pre-order on Amazon. It's scheduled for release in mid-September, and will be around 570 pages long. This release, coupled with the Charles Petzold book "Applications = Code + Markup" which is supposed to be available a week earlier (and is also can be pre-ordered) will no doubt bring some great new perspectives on WPF development from these two excellent technical authors. »

Author image

How do I use a custom font in my WPF application?

Windows ships with a number of attractive fonts pre-installed, however there are many other beautiful and distinctive fonts that are not. Rather than limit your UI design to only the default fonts, WPF makes it easy to distribute custom fonts with your application. I saw this technique on Filipe Fortes' Mix06 video, and subsequently learned he's created a screen-cast showing how to add and use custom fonts. Here's a summary: Including the Font in your Application Include the font in your application adding it to your visual studio project. »

Author image

How do I format Numbers and Dates when using WPF Data Binding?

It is often desirable to control the format of dates, times, money and percentages when they are displayed. The example screen-shot below shows a list of 3 sale records. The sale information includes an Id, the sale amount, the percentage of sales tax paid and the time of the sale. Although the information displayed to the user is correct, the unformatted data might be difficult or confusing for a user to read. »

Author image

WPF-related sessions from Mix06 available for Download

All the sessions from this year's Mix06 conference are now available for download. Here are links to most of the WPF-related sessions. If you see something I've missed please leave a comment. Building a Real World WPF Application: The North Face In-Store Explorer A Designer's Overview of Windows Presentation Foundation A First Look at Windows Presentation Foundation Everywhere (WPF/E) Developing a Windows Presentation Foundation Application Windows Presentation Foundation: The Opportunity for WPF Applications The links above go directly to the downloads (each of which is about 180 MB in size), but there are also streamed versions of the presentations, as well as just the powerpoints at http://sessions.mix06.com. »

Author image

How Do I Add a Splitter to my WPF Window?

Splitters are a useful UI feature where the width or height of a control on the form can be modified to show more or less information. An Example of this is the splitter between the folder tree-view in and the file and subfolder detail view in Windows Explorer, allowing the size of the folder tree-view on the left-hand side of the form to be sized up to 50% of the form width. Windows Forms 1.1 included a Splitter control, and Windows Forms 2.0 included a SplitContainer. WPF does not have equivalent versions of either of these controls; however it is possible to add splitters to your WPF application using the WPF Grid, and a GridSplitter. The example code below allows the two columns in the grid to be re-sized. This is done by adding a GridSplitter as a child item of the Grid.

Xaml Code (Feb 2006 CTP)

<Window x:Class="LearnWPF.Splitter.Window1"
    xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation "
    xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml "
    Title="LearnWPF.Splitter" Height="300" Width="300"
    >
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
      </Grid.ColumnDefinitions>
      <StackPanel Background="#feca00" Grid.Column="0">
        <TextBlock FontSize="35" Foreground="#58290A"
                   TextWrapping="Wrap">Left Hand Side</TextBlock>
      </StackPanel>
      <GridSplitter/>
      <Border CornerRadius="10" BorderBrush="#58290A"
              BorderThickness="5" Grid.Column="1">
        <TextBlock FontSize="25" Margin="20" Foreground="#FECA00"
                   TextWrapping="Wrap">Right Hand Side</TextBlock>
      </Border>
    </Grid>
</Window>

This gives the following UI behavior:

Resizing Grid Columns using a GridSplitter in WPF

The splitter appears "docked" on the right edge of the cell that contains it. Like other items contained in a grid, the splitter can be positioned in a column/row other than the first one by setting the Grid.Column and Grid.Row properties. In the above example it defaults to row and column 0 because these have not been explicitly set.

To allow the GridSplitter to resize cells vertically is somewhat non-intuitive as described by Charles Petzold here. The GridSplitter has a ResizeDirection which defaults to Columns but can be set to Rows to allow the splitter to change the height of a row in the grid. To enable the height of row to be resized correctly by the splitter you also need to set the HorizontalAlignment and VerticalAlignment for to align it to the bottom, and across the width of the containing cells. You can also set the Grid.ColumnSpan for the splitter to span across all the cells in the row that it resizes, otherwise the splitter will only fill a single cell.

This example below creates a 2x2 grid of cells, with a splitter spanning both columns and allowing the height of the rows to be changed.

Xaml Code (Feb 2006 CTP)

<Window x:Class="LearnWPF.Splitter.Window1"
    xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation "
    xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml "
    Title="LearnWPF.Splitter" Height="300" Width="300"
    >
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition />
      </Grid.RowDefinitions>
     
      <StackPanel Background="#feca00" Grid.Column="0" Grid.Row="0">
        <TextBlock FontSize="35" Foreground="#58290A"
                   TextWrapping="Wrap">Left Hand Side</TextBlock>
      </StackPanel>

      <GridSplitter ResizeDirection="Rows"
                    Grid.ColumnSpan="2"
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Bottom"
/>

      <Border CornerRadius="10" BorderBrush="#58290A" Grid.Column="1" Grid.Row="1"
                    BorderThickness="5">
        <TextBlock FontSize="25" Margin="20" Foreground="#FECA00"
                   TextWrapping="Wrap">Right Hand Side</TextBlock>
      </Border>
     
    </Grid>
</Window>

Resizing Rows with a GridSplitter in WPF

»

Author image

Channel9 Interview on the origins of WPF

Channel9 recently interviewed former DHTML Dude and now General Manager of the Windows Client Division Michael Wallent on the origins of WPF. The history of WPF and how the decisions made in its evolution shaped what it looks like today are very interesting. There were also a number of hints as to what we might expect in the future. Here are some notes in case you don’t have time to watch the whole video. »

Author image