[WPF] Formatting Content Controls

Over the past few years I haven’t done much UI work and what little I did was either with a proprietary Web framework or Windows Forms so I’m more than a little behind the curve with WPF. My most recent project has me learning WPF on the fly and although it’s a daunting task and I still have plenty to learn I’m finding it pretty straightforward. Every once in a while though something works completely different than I expect and throws me for a bit of a curve.

One such example just happened yesterday. I was trying to bind and format a value to a Button‘s Content property. I was trying to bind using the same syntax as binding to the Text property on a TextBox:

<Button Content="{Binding Percentage, StringFormat={}{0:P0}}" />

I was surprised though when the value bound properly but wasn’t formatted. After some investigation I learned that controls like Button that inherit from ContentControl can’t be formatted that way. Instead we need to use the ContentStringFormat to achieve the same effect:

<Button Content="{Binding Percentage}" ContentStringFormat="P0" />

With that change the value started showing up as the percentage just like I expected. Lesson learned.


My IndyTechFest Experience

This past Saturday I, along with 400+ developers, admins, and DBAs attended IndyTechFest.  It was a long, intense day of sessions covering topics such as WPF, Silverlight, SQL Server, C#, VB, Testing, and Windows Phone 7.  I’ve had a few days to digest what I heard and wanted highlight some things from each of the sessions I attended.

This year’s conference was split into seven tracks each with five sessions and an all-day open space.  All of the tracks had at least one topic I was interested in and many time slots had conflicts but ultimately I stayed within the general .NET and Silverlight tracks.  My schedule for the day was:

  • Keynote: Are My Three Screens Cloudy?
  • WPF for Developers
  • Implementing MVVM for WPF
  • The State of Data Services: Open Data for the Open Web
  • C# Tips and Tricks
  • Silverlight Code Survey

For the most part I found value in each of the sessions I attended.  Thanks go out to the sponsors, organizers, and volunteers that made this event possible.

Keynote: Are My Three Screens Cloudy?

Presented By: Jesse Liberty

In many ways Jesse Liberty’s keynote was the highlight of the day.  I think my #1 takeaway for the day is that Jesse Liberty is awesome!  In the keynote Jesse briefly described his position within Microsoft, how he got there, and gave a quick history on the evolution of Silverlight.  He went on to describe what Microsoft sees as the “three screens” (computer, TV, and phone) and how Silverlight is the technology that will bring the three screens together through Windows, XBox360, and Windows Phone 7.

WPF For Developers

Presented By: Phil Japikse

This was the first of two Windows Presentation Foundation (WPF) sessions from Phil Japikse.  In this session Phil gave a good introduction to WPF for the non-initiated (like me).  He started by defining WPF, describing the advantages and disadvantages of WPF to WinForms, and discussing new features in .NET 4.0.  The majority of the session was demonstrating some of the more common features.

Some highlights:

  • Creating custom spell-check dictionaries with .lex files
  • Panels dock in XAML order
  • Controls tab in XAML order by default
  • INotifyPropertyChanged interface
  • INotifyCollectionChanged interface

The presentation and example code are both available on Phil’s Samples and Presentations page.

Implementing MVVM for WPF

Presented By: Phil Japikse

Expanding upon his first WPF session, Phil discussed how to implement the Model-View-ViewModel (MVVM) pattern in WPF.  This session was almost entirely demo showing the classes that represent each part of the pattern and how they interact.

The presentation and example code are both available on Phil’s Samples and Presentations page.

Additional Resources:

The State of Data Services: Open Data for the Open Web

Presented By: Dan Rigsby

Dan Rigsby gave a great introduction to OData, a protocol developed by Microsoft to facilitate data interchange between systems using existing Web technologies.  He started by describing REST and Atom/Pub, two technologies that make OData possible then went on to show OData in action.

REST (http://en.wikipedia.org/wiki/Representational_State_Transfer)

  • Embrace the URI
  • HTTP Verbs (GET, POST, etc…) translate to methods
  • Content-Type defines the object model
  • Status code is the result

Atom/Pub (http://atompub.org/)

  • Standards based XML syndication format for publishing and editing web resources
  • Preserves metadata
  • Provides constructs

OData (http://www.odata.org/)

  • “Open” Data
  • Formerly known “Astoria” and ADO.NET Data Services
  • Open protocol
  • WCF Data Services is Microsoft’s provider for creating and consuming OData
  • Netflix provides an OData interface to its video library

C# Tips and Tricks

Presented By: Mark Strawmyer

With all due respect to Mr. Strawmyer I was incredibly disappointed by this session.  The IndyTechFest program had this to say about the session:

This C# presentation focuses on tips and tricks for the C# developer.  It contains a mixture of C# specific features along with other handy how-to items such as shortcuts for working with the C# IDE that will make you more productive.

This session did not fit the description.  I understand the the previous session was a C# 4.0 overview and there was a strong desire to avoid duplication of information but only two of the tips/tricks mentioned were actually specific to C# and one of those was a C# 4.0 feature!

For the curious, the tips and tricks discussed were:

  • Optional & named parameters
  • Extension methods
  • ObsoleteAttribute
  • GC.Collect()
  • using keyword
  • Parallel extensions
  • Utilities

I really question offering up GC.Collect() as a tip, especially when it was provided with the caveat “you can do this but don’t.”  Is letting people know something is possible really a tip if it shouldn’t be done or is not doing it the tip?

To me, a C# tips and tricks presentation should include things such as lesser known/used operators, XML documentation & IntelliSense, compiler options, automatic properties, etc…

Silverlight Code Survey

Presented By: Jesse Liberty

This session was originally going to be “Application Development with Silverlight 4” but after some feedback from the morning’s keynote and the overlap with the MVVM session it was changed.  In this session Jesse did a quick run-through of creating a new Silverlight application, showing some basic data binding, and some basic animation.  Most of the demonstration is available on the Learn page of Silverlight.net.  Nevertheless, I wasn’t about to miss the opportunity to listen to Jesse present again.

In the Hallway

As with just about any conference lots of interesting things happen in the hallway between sessions.  I’ll sheepishly admit that I didn’t use this time as well as I could (should?) have but I really did enjoy playing with the Windows Phone 7 demo application at the Microsoft booth.  I was even clever enough to crash the app by clicking the emulator’s home button while a dialog box was open :)