Silverlight Fluent Templates

The goal of this project is to help provide a richer interface for writing custom controls that utilize Template Parts. The first method of attack is providing an easy way to require certain parts, wireup events and assign types. Utilizing a common container class TemplatePart<T> FluentTemplates can build a chained interface for getting your control up and running. Due to Silverlight locking down any access to internal members and methods FluentTemplates will does have a few limitations. To really take advantage of the library you'll need to either implement an interface, inherit from a class or expose your internals.

Currently this is a source code only project. When I feel like it's alpha/beta quality I will make a binary download available. For now consider this library musings of a silverlight control developer.

No Modifications Quickstart

Fluent Templates doesn't require you to change your controls. The As<T>() extension method will return a TemplatePart<T> that can then be used to Require, DependsOn, WireUp
protected override OnApplyTemplate()
{
     submitButton = GetTemplateChild(ElementNames.SubmitButton)
                            .As<Button>()
                            .DependsOn(nameBox)
                            .WireUp(b => b.Click += (sender, e) => RunSave());
}

Inherited Control

If you'd like to avoid using the As<T>() method you can inherit from TemplatedControl
 public class TestFluentControl : TemplatedControl
    {    
        private TextBox nameBox;

        public override void OnApplyTemplate()
        {
            nameBox = GetTemplateChild<TextBox>(ElementNames.NameBox)
                           .Required()
                           .WireUp(x => x.BindingValidationError += BoxValidationError);
        }
    }

Attribute Driven

Another alternative is using an Attribute approach. This however relies on your template properties or fields to be declared as protected and your assembly to be set to InternalsVisibleTo fluent templates
//In your Assembly.cs
[assembly: InternalsVisibleTo("FluentTemplates",AllInternalsVisible=true)]

// your control
 public class AttributeTestControl : Control, ITemplatePartBinding
    {
        [TemplatePartBinding(Name = ElementNames.NameBox, Required = true)] 
        internal TextBox _nameBox;

        [TemplatePartBinding(Name = ElementNames.SubmitButton, Required = true)] 
        internal Button _submitButton;

      

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (DesignTools.IsInDesignModeStatic)
                return;

            TemplatePartBinding.BindTemplateParts(this);
        }

        public object GetTemplatePartBinding(string partName)
        {
            return GetTemplateChild(partName);
        }

    }

Last edited Jul 14, 2010 at 5:05 AM by DarthObiwan, version 2