Longhorn/XAML Property aliasing
Since so many people are talking about using the ContentPresenter to bind data via an alias, I figured it would be a good time to mention the IDataTransformer interface.
All you have to do is create a class to implement IDataTransformer, (and hit [tab] to insert the method stubs, if you're in Whidbey) and you're good to go with base functionality.
Inside your bound property, just change the declaration a bit:
<SomeTag SomeProperty=“*Bind(Path=SomeDataBinding; Transformer=MyWordLengthTransformer)“ />
And inside your MyWordLengthTransformer : IDataTransformer class, you'll need to change the Transform method to suit your needs. In this rather primitive sample, you'll see that it transforms a string into a string length
public object Transform(object o, DependencyID di, CultureInfo culture)
{
string originalPropertyValue = (string)o;
return originalPropertyValue.Length;}
For an added bonus of reuse, you can put it inside your <Window.Resources> or <Canvas.Resources> (etc.) tags as shown below:
<TransformerSource def:Name="MWLTransReference" TypeName="MyWordLengthTransformer"/>
and when you use the declaration in the XAML element, you'll use:
<SomeTag SomeProperty=“*Bind(Path=SomeDataBinding; Transformer={MWLTransReference})“ />