So now I'm a published author.
Well, not *EXACTLY*!
Well, not *EXACTLY*!
Setting up the template for a new custom control in Silverlight is fraught at the best of times – the Silverlight runtime invariably swallows any error in the template and gives just a cryptic exception.
But stranger still is a limitation on how the {TemplateBinding ...} syntax can be used and the exception that is thrown when in error.
The {TemplateBinding ...} syntax provides a quick and simple way to bind properties within a control’s template to the properties of the control itself. According to the MSDN documentation, it’s a shortcut to the more full-featured {Binding ...} syntax. But what’s not clear from the documentation is that {TemplateBinding ...} can only bind a control property to a DependencyProperty on the control class.If you try and use {TemplateBinding ...} against a normal property on the control, you get the strange exception shown right – confusing because the target control (WizardActionButton in the example) absolutely does have a State property.
The solution is to use the {Binding ...} syntax where the source property isn’t a DependencyProperty, and to use the {RelativeSource ...} syntax to specify that you’re actually binding to the control within its template.
Wrong:
<i4tControls:WizardActionButton x:Name="RetreatButtonPart"
Style="{StaticResource ActionButtonStyle}"
State="{TemplateBinding CanRetreat}"
Content="{TemplateBinding RetreatButtonContent}"
ContentTemplate="{TemplateBinding RetreatButtonContentTemplate}"
/>
Correct:
<i4tControls:WizardActionButton x:Name="RetreatButtonPart"
Style="{StaticResource ActionButtonStyle}"
State="{Binding Path=CanRetreat,RelativeSource={RelativeSource TemplatedParent}}"
Content="{TemplateBinding RetreatButtonContent}"
ContentTemplate="{TemplateBinding RetreatButtonContentTemplate}"
/>
Labels: Quickies, silverlight
This should be old news to most readers (it was announced all of 3 days ago!), but bears re-posting.
Scott Guthrie is presenting in Manchester
on 29th September!
Why haven’t you registered already?
Gu’dness: http://developerdeveloperdeveloper.com/guathon/Default.aspx
This is another really simple and obvious code snippet, but one well worth remembering – posted this morning by Fabrice MARGUERIE.
The crux is that event subscriptions can cause memory leaks – so ensuring they are un-subscribed is essential. The code snippet is designed to be used in the Dispose or Cleanup method on the publishing object, and forcibly disconnects any subscribers just before the publishing object is disposed.
if (SomeEvent != null)
{
foreach (EventHandler handler in SomeEvent.GetInvocationList())
SomeEvent -= handler;
}
Simple. Clean. Elegant. Use it!
Force your subscribers away: http://weblogs.asp.net/fmarguerie/archive/2009/09/09/forcing-event-unsubscription.aspx
Came across this little article this afternoon via a tweet from @DaveSussman giving some background and justification for using rounded corners in your UI design. Well worth a read - I particularly like the examination of Apple hardware from a rounded corner point of view.
Round your rectangles: http://www.uiandus.com/2009/07/27/theories/realizations-of-rounded-rectangles/
Spotted this great little article today on how to use the Mediator pattern to allow you to bind an animation to a scroll-viewer to get really slick scrolling.
Smooth as butter on a muffin: http://blogs.msdn.com/delay/archive/2009/08/04/scrolling-so-smooth-like-the-butter-on-a-muffin-how-to-animate-the-horizontal-verticaloffset-properties-of-a-scrollviewer.aspx
This morning, @DaveSussman (http://blogs.ipona.com/davids) posted a link on Twitter to an excellent set of CSS resources:
This post is just an aide-memoire... and to share the CSS goodness.
This little tweet turned up this morning in response to Mike Taulty wanting“the ability to fire an event without having to check it for null”.
| richardblewett @mtaulty you can use the null pattern for this Mike, just do = delegate{}; on the event declaration |
It’s pretty obvious when you think about it tho’...
public event EventHandler CriteriaChanged = delegate{};
Easy-peasey.
Update: Richard credits Andy Clymer the original idea (see comments). How far will the chain go?!
Labels: C#, EventHandler, Twitter