Yet another submission to The WPF Challenge

February 27, 2007

A fellow by the name of Bryan Livingston recently submitted an XBAP into The WPF Challenge. It is a vector graphics editor which has some cool functionality, especially the huge range of text rendering options.  You can check it out here and then click the “Logo Designer” link.

Feel free to peruse all of the submissions on the WPF Challenge Submissions page.

WPF.JoshSmith has been updated

February 25, 2007

I’ve been busy tonight updating some of my articles on CodeProject.  There is a new version of the WPF.JoshSmith library, which you can grab here.  In addition to some bug fixes, I also added the SmartTextBox control to the library.

By the way, the “Josh Smith on WPF” t-shirts arrived in the mail the other day.  They look great!  You can win one for yourself (along with thousands of dollars worth of WPF components) by entering The WPF Challenge.  😉

Three things to read

February 25, 2007

I fixed a bug in my SmartTextBox class and updated the Spelling Suggestions in a WPF TextBox article on CodeProject.  Refer to the “Revision History” section at the bottom of the article to see what changed, if you care.  Zhou Yong (aka Sheva) and Ian Griffiths figured out the problem and explained the solution to me.  Thanks a lot guys!

Along the way, I encountered some great WPF reading material by those two guys.  Sheva summarized the solution to the problem I faced in his Visual Level Programming vs Logical Level Programming post.  Very helpful info.

Ian recently blogged about an interesting aspect of styles and control templates, which really sheds some light on the topic.  Check out his Default Templates in WPF post to see what he has to say.

Stretching Content in an Expander Header

February 24, 2007


Long after this post was written, a fellow by the name of Patrick Jones left a comment which showed another way to implement the functionality shown in this post.  Upon reading his clever implementation, I realized that this is entirely possible to achieve without writing a single line of code.  The trick relies on binding the Expander’s header element’s Width to the ActualWidth of the Expander.  Here’s a demo:

      Text=”I am header text…”
          AncestorType={x:Type Expander}},

  <TextBlock Background=”Red”>
    I am some content…

Note: This technique makes the header element wider than the available space (it does not subtract out the width of the Expander’s expansion indicator).  If your header element has a border around it, such as a Button, this XAML-only technique will not work properly because the header element is clipped on the righthand side.  If you need the header element to be exactly the right width, then you will need to use the approach explained below.


Someone recently asked me how to stretch the content of an Expander’s header.  By default the Expander will force the header’s content to “auto-size” itself, like this:

Default Expander header (unstretched)

In the screenshot above, a DataTemplate was applied to the Expander’s HeaderTemplate property.  That template consists of a Border with a blue background wrapping a ContentPresenter.  Notice that the Border does not extend all the way to the right edge of the header area.  The desired effect is to have the header content stretch, like this:

Stretched Expander header content

I first tried to make the header content stretch by setting properties on the Border in the DataTemplate.  No matter which properties I set it had no effect, so I realized that a little Snooping was necessary to get to the bottom of this.  I fired up Snoop and soon realized why I could not stretch the header content.  The ContentPresenter which hosts the expanded HeaderTemplate elements (note, ‘expanded’ in the sense of ‘instantiated’) has its HorizontalAlignment explicitly set to ‘Left’.  D’oh! 

Here’s what I’m referring to, when using the Expander’s default HeaderTemplate:

Snooping around in the default Expander visuals
(click me)

The question now is: how can we set that ContentPresenter’s HorizontalAlignment to ‘Stretch’?

I know of two good ways to go about setting that property.  We could get a copy of Expander’s default control template (for each OS theme…), find the ContentPresenter in the template, set the HorizontalAlignment to ‘Stretch’, and then use that modified control template in our application.  For you XAML-only zealots out there, that’s the way to go.

Alternatively, if you are not restricted by a XAML-only rule, you can perform a little kung fu in the code-behind to make it happen.  Here’s the magic:
How to stretch the content of the Expander’s header
(click me)

You can download the demo project here.  Change the file extension from .DOC to .ZIP and then decompress it.

New submission to WPF Challenge

February 21, 2007

A fellow by the name of Chris Cavanagh submitted an XBAP application which demonstrates a simple physics engine, written in C#.  You can read more about it here:  Try it out on this page:

Note, to move the “vehicle” around, use the Left and Right arrow keys.  The Up and Down arrow keys have an effect, too.  Reset the whole thing by pressing F5. 

Keep in mind, since the WPF Challenge has been changed, you do not have to submit your app as an XBAP.  It is perfectly OK to do so, but feel free to submit the source code for a WPF desktop application, too.

 Nice work, Chris!

Spelling Suggestions in a WPF TextBox

February 19, 2007

I just posted a new article to the CodeProject.  It shows a class I created called SmartTextBox.  That class provides an intuitive way to display spelling suggestions for misspelled words.  If you want to read it:

The WPF Challenge has been changed!

February 19, 2007

I have modified the rules of The WPF Challenge.  You no longer are required to submit a link to a live XBAP.  Instead, you can now post a link to a .ZIP file which contains your WPF application source code (XBAP or desktop).  Do not submit an EXE.  I am concerned that people will have a difficult time finding a Web host which supports XBAP files on their servers, and I don’t want people to not submit based on a mundane roadblock.

To recap, you can submit any kind of WPF application now.  It does not have to be an XBAP!

The floodgates have opened!  Let the mayhem begin…