Archive for February, 2009

Write in C

February 26th, 2009

Silverlight ComboBox Default Style

February 24th, 2009

A few days ago I wrote about learning from the default styles of various Silverlight controls as posted on the MSDN here.  Guess what?  They forgot the ComboBox style, which I found after a little searching.  Enjoy.

Poor Silverlight Documentation

February 17th, 2009

Microsoft has done a rather poor job of properly documenting Silverlight 2.0. I have run across several outright errors in the official MSDN documentation, and finding acurate documentation at other sources is very difficult. Today I am trying to style the header elements of a Silverlight DataGrid. The documentation for this is scant and wrong. Luckily I have found the default styles used out-of-the-box by Silverlight. Using the default styles, I figured out exactly where I was going wrong, and where various documentation I had read was wrong. There is nothing like learning by example.

Silverlight Toolkit and Developing Silverlight Controls

February 14th, 2009

I have been spending a significant amount of time in Silverlight lately.  Silverlight is very young.  The base technology is solid but it is not as featured as more mature web technologies like ASP.Net.  Being young also means that there are not a lot of 3rd party libraries available for a variety of tasks.  So I have been writing a lot of framework and component code for our prototype (and possibly production) code.

After a few attempts at developing a clean user control for what should have been something that is very simple, are started looking at the Silverlight Toolkit source code to see if I could garner any best practices from the work Microsoft is doing.

The Silverlight Toolkit team has done some interesting work setting up their project.  Instead of developing the toolkit controls with the standard UserControl base class, they have instead opted to write classes based on ContentControl and other base classes.  To pair up XAML with the “code behind” they did something really interesting.  They engineered a pre-compile task to take XAML resource dictionaries and combine them into one resource dictionary for the project, the so called “generic.xaml” dictionary that provides styles and templates to control libraries (think DLL).  I won’t go into a thorough explanation on this page (though I do explain the basic below), but I would invite you to check out the source code and take a look at the explanation of how it all worked here.

It really cleaned up my code.  The Silverlight Toolkit approach eliminates a lot of C# code and replaces it with declarative markup.  This is a good thing because C# code can have bugs, where declarative markup usually works or it doesn’t.  One must test for edge cases in C#, markup usually works or it doesn’t.  Get get the picture.  That, and for my specific cases it eliminated some really horrible code patterns.  The patterns are much simpler now:

  1. Write dependency properties and their associated ROC (Regular Old Class) properties to represent various items that need to be represented or displayed in UI.  For example, child components, styles, colors, brushes, text, etc, would all be loaded into DPs.
  2. Initialize DPs through ROCs in the control constructor with the default values.
  3. Set the DefaultStyleKey of the control to the type of the control.
  4. Template the control with a style/control template combo in a resource dictionary that will get compiled into the DLL.  The Style will be wired up to the control because the TargetType is used as the default style key set in #3.  Use the TemplateBinding XAML markup to bind the the DPs.
  5. Rinse, repeat.

The first controls I developed were with the traditionaly “Silverlight User Control” template were full of spaghettie code trying to manage child controls.  Since then I have used the Silverlight Toolkit approach, and I have been writing much better code.

Foxes On A Trampoline

February 12th, 2009

JasonJackson.com

February 9th, 2009

I periodically get an email from someone wishing to buy my domain name, JasonJackson.com .  Since my name is Jason Jackson (TM), I don’t quite get any of the requests and always say no.  Anyone else named Jason Jackson is obviously a copy, even if born before me.

The great thing about this domain is that even though it says it expires in 2012, I purchased the 100 year plan. I will always be the real Jason Jackson, and all others will be imitations.  Well, at least for the next 98 years.  I wonder what I will be doing in 2107?

ContentPropertyAttribute

February 9th, 2009

While diving into the programming of custom controls in Silverlight I wanted to set one of my properties as the “default property” from XAML.  What I mean here is that I wanted to emulate the behavior of many out-of-the-box controls in Silverlight (and WPF for that matter) where a property need not be explicitly specified, in all cases a content property of some sort.  For example, the Button control has a Content property which can be set in these three ways (excluding binding):

  1. <Button Content=”Click Me”/>
  2. <Button><Button.Content>Click Me</Button.Content></Button>
  3. <Button>Click Me</Button>

The relationship between these different ways of setting Button.Content should be fairly obvious.  Through XAML one can usually set any property on the underlying object via an attribute or child element.  Additionally, one property can be marked as the “default property”.  But how is this done?  How can I write a control and mark one of the properties as the “default property”.

In XAML/Silverlight/WPF parlance, this is refered to as the Content Property, which is confusing since there is also a property on a number of controls called content, and to make it even more confusing the property named Content is also the Content Property.  Confused?  Lets take a swing at this using my custom control and see how to set the Content Property on a control with no property called Content.

I have a control that doesn’t have “content” per se, but accepts a list of rules which regulate how the control should act.  I want the XAML to accept this list as the “default property”.  I want to be able to write:

<RuleManager>
  <Rule ... />
  <Rule ... />
  <Rule ... />
</RuleManager>

instead of the more verbose:

<RuleManager>
  <RuleManager.Rules>
    <Rule ... />
    <Rule ... />
    <Rule ... />
  </RuleManager.Rules>
</RuleManager>

The way I do this is with the ContentProperty Attribute in System.Windows.Markup:

[ContentPropertyAttribute("Rules")]
public class RuleManager
{

public static readonly DependencyProperty RulesProperty =
DependencyProperty.Register(“Rules”,
typeof(RulesCollection),
typeof(RuleManager),
new PropertyMetadata(null));

public RulesCollection Rules
{
get { return (RulesCollection)GetValue(RulesProperty); }
private set { SetValue(RulesProperty, value); }
}

public RuleManager()
{
Rules = new RulesCollection();
}
}

The ContentPropertyAttribute class should probably be renamed something like DefaultXamlPropertyAttribute since it does not always specify Content, but does specify the default property.  Its constructor takes the name of the property to use as the default (or "content") property. It took some digging, but this is the correct way to do it and it is tested working.  From XAML I need not specify the Rules element (aka property); the XAML parse will assume that information included here is mean for the Rules property based on the attribute.

MSDN documentation of ContentPropertyAtrtibute in Core CLR (Silverlight)

Clinton, Bush and Washington are on a sinking ship

February 8th, 2009

Bill Clinton, George W. Bush and George Washington are on a sinking ship.

As the boat sinks, George Washington heroically shouts, “Save the women!”

George W. Bush hysterically hollers, “Screw the women!”

Bill Clinton’s asks excitedly, “Do we have time?”

Source

WGN News

February 7th, 2009

99 Problems

February 5th, 2009