« August 2007 | Main | October 2007 »

September 2007

Saturday, September 29, 2007

Silverlight, custom controls and setting width and height

I guess it's just one of those facts of life that sometimes beta bits turn on you and bite you in the ass. I've been working on a set of charting-related custom controls for both WPF and Silverlight that would let you produce some nice looking sparklines and bullet graphs, and there were some issues in the Silverlight controls that had me stumped. I tried to make the code somewhat portable between WPF and Silverlight (not as easy a feat as one would think), and one of the latest problems I've run into is related to setting the Width and Height of my control in XAML.

As a basis for my work I'd been using one of the quickstarts at Silverlight.net, namely the on creating a custom control. This puppy was clearly written before the latest refresh of Silverlight 1.1 and the tools for Visual Studio 2008 beta 2.

I had my custom control shadow the width and height properties, like this:

public virtual new double Height
{
get { return _implementationRoot.Height; }
set
{
_implementationRoot.Height
= value;
UpdateLayout();
}
}

public virtual new double Width
{
get { return _implementationRoot.Width; }
set
{
_implementationRoot.Width
= value;
UpdateLayout();
}
}


Where _implementationRoot was declared as:


private FrameworkElement _implementationRoot;

and I was initializing it in the constructor of my custom control like this:


public BulletGraph()
{
System.IO.Stream s
= this.GetType().Assembly.GetManifestResourceStream("SilverlightBulletGraph.BulletGraph.xaml");
_implementationRoot
= this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());
}

The reason for doing this was that I wanted to be able to re-layout my control if its size changes. This should have been all fine and dandy, as it was all in line with what the sample from Microsoft was saying. But, when I was trying to perform a lot of layout magic that was needed in order for the bullet graph control I was trying to implement I noticed that Width and Height were never being set, despite the fact that I in the XAML file I was declaring them like this:


<my:BulletGraph Width="300" Height="100"
Header
="SalesSales" Scale="EUR"
Minimum
="0" Maximum="150" TickFrequency="25"
Value
="87" Target="100"
BadRange
="50" SatisfactoryRange="80"/>

So, as it turns out, the Silverlight refresh and Visual Studio 2008 beta 2 (Orcas) has a bug in them (surprise, surprise!). The Width and Height never get called.


Unfortunately I kept trying to solve this on my own too long before relying on Google. If I'd done so I would have come across several pieces of advice, among those that of Jeff Prosise of Wintellect fame, who blogged about this in an article back in August.


Anyway, as it turns out, the advice given in the original article, is no longer valid for the refresh. Width and Height never gets set, and I ended up removing the properties. This way I can't achieve all of the behavior I was looking for, since if the size changes I won't find out. I'll wait for the next version of Orcas and the Silverlight tools, where I'm sure Microsoft has fixed this-


 


And, by the way, if you're wondering what all this bullet graph stuff is about I seriously recommend that you check out the writings of Stephen Few (who invented them), at Perceptual Edge or at wikipedia.


And the controls I've been making? I hope to polish them a little bit and post more about them during next week.

Saturday, September 15, 2007

Way more nerdy than I thought

I stumbled across the Nerd Test through Chris Sell's blog, and here is the outcome:

NerdTests.com says I'm a Cool Nerd God.  What are you?  Click here!

I don't know if being a "Cool Nerd God" brightened up my day or utterly destroyed it...

[Addendum (13:35): Great, wifey just told me that the actual nerd test was whether you felt compelled to take the test or not...]