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…

Looking for a server to host XBAPs

February 19, 2007

Chad Campbell raised a good point about The WPF Challenge.  It’s not easy to find a web host which supports XBAPs yet.  It would be great if there was a common server on which XBAP submissions could be kept.

If you know of a Web host whose servers support XBAPs, please post a comment to this blog entry and provide us all with a link to it.  Ideally it would be free, but inexpensive ones are OK too. 

Thanks for any help!

Spellchecking and Suggested Spellings in a TextBox

February 17, 2007

WPF has built-in support for spellchecking.  Unfortunately the SDK docs on the spellchecking features suck (to put it nicely).  For example, it never even mentions which languages are supported by the spellchecker!

I just spent a few hours trying to come up with a slick way to provide spelling suggestions for misspelled words.  I put the suggestions (which are provided by the spellchecking APIs) into a ListBox and then showed it in a Popup.  After fiddling around trying getting it “just right” I discovered that the functionality already exists.  The docs just never mention it…

The built-in support is not quite as cool as what I was trying to do, but here’s what the provided functionality looks like:
Spellcheck in action

Notice that if you bring up a context menu on a misspelled word, the menu lists suggestions for that word.

One way to enable the spellchecking functionality on a TextBox or RichTextBox is by setting SpellCheck’s IsEnabled attached property to true, like so:

<TextBox SpellCheck.IsEnabled=”True” />

Here’s a snippet of the code I was working on before I made the discovery.  It takes the suggested words and shows them in a ListBox.  This code would be useful if you wanted a portion of the UI to display suggestions, without requiring the user to open a context menu.

if( this.textBox.CaretIndex > 0 )
 int idx = this.textBox.CaretIndex – 1;
 SpellingError error = this.textBox.GetSpellingError( idx );
 if( error != null )
  this.listBox.ItemsSource = error.Suggestions;

WPF Ribbon control

February 16, 2007

DevComponents announced that they have developed a WPF Ribbon control, as seen in Microsoft Office 2007!  It looks really great, and (as far as I know) it’s the first one to hit the market.  You can check it out here.