Improvements made to my MVVM commanding solution

July 26, 2008

I woke up extra early this morning and just had to implement some enhancements to my recent MVVM commanding solution.  I took Bill‘s brilliant advice and made a concrete CommandSink class. That alone is a HUGE improvement!  I made a fix that allows the CommandSink attached property to work for elements in a template.  This is related to the timing issues Bill and I discussed, wherein there is no guarantee that my attached property will be invoked *after* an element’s command bindings are created and added to its CommandBindings property.  Also, I renamed RelayCommandBinding to CommandSinkBinding because I think that more accurately expresses the purpose of the class.

The article has been updated to explain the new features.  All of the important changes are in the How It Works section.  Also, the source code download was updated.  Here’s the link:


Article about using routed commands with MVVM

July 24, 2008

I just published an article on CodeProject that discusses a technique which simplifies the use of routed commands in an MVVM architecture.  If you’re interested, here is the link:

Enable ElementName Bindings with ElementSpy

July 22, 2008

There are some situations in WPF where the ElementName property of a Binding is useless.  One common example of this is when you try to perform an ElementName binding on elements in a tooltip.  WPF manages to pump an inherited DataContext into the elements in a tooltip, but it does not add a tooltip to a namescope, so you cannot use ElementName in their bindings.

If you try to use an ElementName binding on a tooltip element, the Output window will display this type of debugging info:

System.Windows.Data Error: 4 : Cannot find source for binding with reference ‘ElementName={0}’. BindingExpression:Path={1}; DataItem=null; target element is ‘{2}’ (Name=”); target property is ‘{3}’ (type ‘{4}’)

There have been some workarounds posted, such as the excellent approach that Andrew Smith posted shortly after we discussed this very issue.  His approach involves a custom implementation of INameScope that traverses the element tree to find a namescope to apply to a tooltip.  There is also the idea of binding to the ToolTip’s PlacementTarget, which still does not give you a functioning ElementName binding, but does allow you to bind to a certain element.  This blog post introduces yet another utility class that leverages the Hillberg Freezable Trick to gain access to an inheritance context from an object added to a resource dictionary.  This is similar to my DataContextSpy, only its purpose is a bit different.  ElementSpy, the new class, provides two ways to enable you to bind to any element in the element tree, from any element that is not able to use ElementName bindings (such as elements in a tooltip).

It is important to note that ElementSpy relies on the use of reflection, so it cannot be used in a partial trust application, such as a standard XBAP.

The ElementSpy class exposes two public properties, Element and NameScopeSource.  Element is a read-only property that returns the element in whose Resources collection the ElementSpy is placed.  You should only put an ElementSpy into one element’s Resources and once it is in a Resources collection, you should not remove it and add it to another element’s Resources.  Here is the Element property.

As mentioned above, the ElementSpy class relies on reflection to get at its InheritanceContext, which is an internal property of Freezable (which is ElementSpy’s base class).   If you have a policy that prohibits the use of reflection to coerce .NET into bending to your wills, you obviously cannot use this class.  When a Freezable, such as ElementSpy, is placed into an element’s Resources collection, the InheritanceContext property is set to that containing element.

The other property is an attached property called NameScopeSource.  That property allows you to assign an element the NameScope that was assigned to the element that contains an ElementSpy.  This property is a tad more confusing than the Element property, but, as we will soon see, it has very strong advantages.  Here is the property definition:

Now let’s see this in use.  The demo app looks like this:

The key point to notice is that the tooltip of the TextBlock displays information taken from properties of the Window.  This is achieved via ElementName bindings.  The beginning of the Window’s XAML looks like this:

It is important to note that we add the ElementSpy to the Window’s Resources, because we want it to “spy on” the Window.  Whatever element’s Resources collection an ElementSpy is added to will be the element returned by the spy’s Element property.  Now let’s see how the upper TextBlock in the demo app uses the Element property to bind its tooltip elements to the Window:

The key is that we reference the ElementSpy as a resource (in this case, a StaticResource) via the binding’s Source property.  Also, the binding’s Path includes the Element property.  ElementSpy exposes that property, which returns whatever element the ElementSpy happens to live in the Resources collection of.  In this example, the Element property returns the demo Window.

Now let’s see how to use the attached NameScopeSource property.  Setting this property once enables multiple elements to use ElementName bindings.  Here is the lower TextBlock in the demo app:

As seen in the XAML above, using the NameScopeSource property enables true use of ElementName bindings.  This works because we are assigning the root TextBlock of the tooltip the same NameScope assigned to the Window.  Since the Window is in that scope and has an x:Name of “window”, the elements in the tooltip are able to find the Window via ElementName binding.

Download the source code here: Element Spy Demo .  Be sure to rename the file extension from .DOC to .ZIP and then decompress the file.

My WPF library has been updated

July 13, 2008

After procrastinating for well over a year, I finally bit the bullet and updated WPF.JoshSmith today.  It took hours and reminded me how much I dislike working with HTML!  😐

I added a bunch of my WPF controls, panels, utility classes, etc.  I did not bother adding demo projects to the solution, since I provide links to the demo projects. Well, actually, I didn’t add demo projects because I’m far too lazy to bother…

So, if you want to have all of my goodies in one project/DLL, you can get it here.

Fifty articles and going strong!

July 5, 2008

I published my fiftieth article on CodeProject today!  😀

When I wrote my first article, which I find rather embarrasing today, back in February 2003, I never imagined that I would write 49 more.  It took me about five and a half years to reach fifty articles, but that’s OK…it’s not a race!  If you are interested, you can see a listing of all my articles here.

I decided to do something special for my fiftieth article.  I won’t bother explaining it here, but trust me, it’s pretty cool (in my opinion)…

Here’s the link:

My first article in MSDN Magazine

July 4, 2008

The July 2008 issue of Microsoft’s MSDN Magazine contains an article I wrote about WPF data binding. The article is called “Customize Data Display with Data Binding and WPF“.  It covers a wide range of data binding techniques that real applications use all the time, from working with hierarchical data sources to input validation.  It even has a Virtual Lab, so you can get your hands dirty with source code while reading the article.  I think the article turned out very well, and hope that you agree!

This is the first time I was paid to write about WPF, and, in the words of Lloyd Christmas, “I like it…I like it a lot!”  😀


Bach to the Future

July 3, 2008

Every once in a while, I post a recording of myself playing the piano.  Today is one of those days!  Please excuse the crappy recording quality, I have no recording/mixing tools (or skills!).  I have been working on the C Minor Prelude from the first book of the Well-Tempered Clavier, by J.S. Bach.  Whenever I know that I’m being recorded, I inevitably make mistakes that never occur when playing sans audience.  I know there must be some analogy involving a tree falling down or Heisenberg here…

Anyways, if would like to hear what my musician alter ego has been up to recently… wtc-book1-cminor-prelude

If you like that piece and would like to see one crazy-talented person play it on two electric guitars…  click here

Synchronized Field Widths in XamDataGrid

July 3, 2008

I tackled an interesting programming task where I needed the width of cells in child records of a XamDataGrid to stay in sync with the width of the corresponding cells in the parent record.  I worked through the problem and ended up with a pretty interesting solution, which I blogged about here.  Eventually the datagrid will provide that functionality out-of-the-box, but until then at least there’s a temporary solution.  WPF is so much fun!

Catalogue of Artificial Inheritance Context Techniques

July 2, 2008

I spent my lunch break today publishing an article that I’ve been working on for a couple of days. It explains and shows examples of three techniques I have developed for creating an artificial inheritance context.  This is useful for binding elements that are not in an element tree to the elements in the tree. If you are interested in checking it out, read “Artificial Inheritance Contexts in WPF“.


The Podder Skinning Competition Deadline has been extended

July 1, 2008

The Podder Skinning Competition deadline has been extended to 00:00 (i.e. 12:00 AM) GMT on Monday, August 4, 2008. Only one person submitted an entry by the initial deadline (several others were submitted several hours too late). Most of the contestants said they just need more time to work on their skins. Since the whole purpose of this contest is to have fun and be creative, I see no reason why we shouldn’t have an extra month to work out the details!

Thanks to Rudi Grobler, the guy who actually submitted a skin on time, for graciously suggesting that the deadline be extended. That was very cool of you, Rudi! 8)

Anyone who has already submitted a skin, don’t worry. I deleted your submission comments so that people can’t steal your awesome ideas! Feel free to keep working on your skin and submit it again when you’re ready.

Let’s have some fun! I can’t wait to see what you come up with…