What is the equivalent of the ASP.NET Repeater in WPF?

The ASP.NET repeater is a great control for creating an arbitrary templated look for lists of data in ASP.NET web applications. WPF's databinding bears a great deal of similarity to the templating support found in the repeater, taking the power of templating even further by allowing templates to be defined as resources and re-used in multiple contexts, and in lots of different types of controls. However in spite of this there is no Repeater control in WPF. »

Author image

How can I provide my own Main() method in my WPF application?

When you create a WPF application in Visual Studio the 'entry point' for the process, where the application is created and the first form displayed, is not directly apparent. Instead the start-up behaviour is controlled by setting attributes on the App.xaml, such as which form to start. At compile-time WPF generates the real entry point method using the classic static void Main() method signature in C#, or Public Shared Sub Main() in VB.NET. Inside the Main() method WPF creates an instance of one of your classes which derive from System.Windows.Application, and calls the blocking Run() method on this Application-derived class which keeps the process "alive". Generally this is a useful abstraction, hiding away some of the lower-level details of how the WPF application starts up, however sometimes you want to provide your own version of the Main() method. So how do you tell WPF to use your main method and not its own?

The way WPF knows to create the Main() method for a particular xaml file is through the build action property set for App.xaml - it has a build action of ApplicationDefinition. By changing this to Page WPF won't create the Main method and you can provide your own in a regular class file you add to the project.

Changing the Build Action for App.xaml

It is important to note that when you provide your own Main() method that the method should be adorned with the STAThread attribute, which sets the threading model for the process. You can either look at the current generated Main() method for your project as a starting point (it should be in the obj folder) or use the following code:

[System.STAThreadAttribute()]
static void Main()
{
    YourApplicationName app = new YourApplicationName();
    app.InitializeComponent();
    app.Run();
}

»

Author image

WPF-specific Snippets in Visual Studio 2008

Visual Studio 2008 comes with some useful snippets for WPF development out of the box. In C# type propa followed by two TAB keystrokes inserts the a snippet for an attached dependency property, including a getter, setter and the registration code. Similarly propdp followed by two TAB keystrokes inserts a snippet for a dependency property. As you would expect the snippet for this is very similar to the attached dependency property, except in this case it calls DependencyProperty.Register() instead of DependencyProperty.RegisterAttached(). »

Author image

How Big is WPF?

One thing people often ask me about WPF is the learning curve - how long will it take for me to be productive, and how long before I know the “whole api”. The sad fact is that knowing the “whole” of WPF is a difficult proposition, due to its size. A lot has been written about the learning curve associated with the XAML programming model and the vector-based retained rendering system, but regardless of these factors WPF is going to have a learning curve due just to its size. »

Author image

Visual Studio 2008 and .NET Framework 3.5 Released

Visual Studio 2008 and .NET Framework 3.5 were released earlier this week. Visual Studio 2008 is the first version of Visual Studio released since WPF and the rest of .NET Framework 3.0 were released a year ago. VS2008 includes a WPF designer and XAML editor, and allows targeting of .NET 3.0 or .NET 3.5 when developing and compiling solutions. .NET Framework 3.5 includes a host of new language features for C# 3.0 and VB9. »

Author image

How can I Change the Size and Color of the Stylus Input in the InkCanvas?

WPF’s InkCanvas control provides a quick and easy way to add ink input to your application. By default the strokes in the InkCanvas are thin black lines, which may be too thin or difficult to see in some instances. Fortunately the appearance of the strokes can easily be changed by modifying the DefaultDrawingAttributes property of the InkCanvas which is of type DrawingAttributes. <InkCanvas> <InkCanvas.DefaultDrawingAttributes> <DrawingAttributes Color="Red" Width="5" /> </InkCanvas.DefaultDrawingAttributes> </InkCanvas> The DrawingAttributes has a number of other interesting properties, such as the FitToCurve property which makes lines drawn in the canvas smoother. »

Author image

How can I have my WPF application start in “kiosk” mode?

For some kinds of applications displaying as “maximised” is not enough, either because they want to make use of every single pixel of screen-space possible, or because displaying the task bar and associated window trimmings would be distracting or confusing for users of the application. The PowerPoint slide viewer is a good example of this, starting in “kiosk” mode to create a more immersive presentation and preventing audience members becoming distracted by the task bar and things in the notification area. »

Author image

Free Vector Images for your WPF Projects

If you're from further down the developer end of the designer-developer spectrum chances are you've been avoiding creating your own images, and on the look-out for some good XAML icons and vector images to include in your application, since bitmap images often look blurry. While we included some XAML image resources in the past there are plenty of other good resources out on the web if you know what to look for. »

Author image

Real-World WPF Start Time Perf Tip - Delay Setting the DataContext

.NET applications traditionally suffer from slower startup time than native applications, caused by the time to load the .NET framework DLLs, JIT time etc. WPF applications have further things that can slow down cold start-time, the extra PresentationCore, PresentationFramework and WindowBase assemblies that need to be loaded and the start-up of the PresentationFontCache service. Any tips to help improve start time is therefore most welcome.

Doug Stockwell recently released an updated version (v1.6 build 2802) of his free RikReader WPF-based RSS Reader, with considerably-improved start time. I've been using RikReader as my primary RSS reader since it was first released, and in previous versions the start time stood out as a blemish on an otherwise excellent app. The performance characteristics of older versions of RikReader were a little different to usual applications, in that the hot start-times were still quite bad (>10 seconds for my list of a couple of hundred feeds), whereas the new version hot-starts in about 4 seconds (roughly equivalent to Expression Blend, Thoughtex and most other WPF applications I use). Cold start-times for RikReader had also improved noticeably.

I contacted Doug to find out what he'd done to achieve this great improvement, and was pleased to hear of the simple change he had made. Instead of using XAML to declaratively set the DataContext for his main window, which contained all the RSS feed data, he switched to setting it programmatically in the Application.OnActivated. This sounds like a tip that would improve the perceived performance of the app (making the main window display more quickly) without improving the actual performance, however it seems to have also improved the actual start time considerably. As with all things performance-related your mileage may vary, and it pays to measure yourself. Fortunately the simplicity of this change means it will be easy to swap between the two methods of setting the DataContext.

»

Author image

Visual Studio 2008 Beta 2, Sliverlight Tools for VS2008 and Expression Blend 2.0 August Preview

This week was a big one in the WPF tools space, following the release of Visual Studio 2008 Beta 2 (in Express, Standard and Pro editions for which you need to download the secure content downloader), the Silverlight tools for Visual Sudio 2008 and Expression Blend 2 August Preview (which features a number of new silverlight-specific features). The Visual Studio 2008 cider designer is much improved from beta 1 - it remembers your settings for each file, crashes less often and is able to render your xaml files more frequently (if this sounds like faint praise - it is). »

Author image