d:DesignInstance in Depth

Update: I have contacted WPF “User education” team at Microsoft about the lack of documentation. The response was: “I’ve looked into this and there was some confusion around whether or not these were publicly exposed. Thanks for letting us know about this error. We are now working on getting these documented.”

There is currently no formal documentation for the DesignInstanceExtension (which you will see in XAML as d:DesignInstance).

The best two articles are by Unni and Karl but there are a few things they don’t cover.

This is what I have worked out by trial and error:

According to Intellisense DesignInstance has got 3 parameters: Type, IsDesignTimeCreatable and CreateList.

Type This is the type of the object for which the design-time data binding “shape” will be derived
IsDesignTimeCreatable (Defaults to false)

If this is true then a new instance of your type will be created at designtime. If you want data to actually appear in the designer you will need to make sure suitable properties are being set in your constructor. The best way to do this is to create a derived type of your model type, where you are free to set properties to default values without polluting your real types. If your model type implements an interface even better – you can create a design time model that way.

If this is false VS/Blend will use reflection to create a dummy type with the same shape as your specified Type. The problem with this is the dummy type will have no instance data in it thus you will not see anything in the designer. When this is false you are really only getting the benefit of assisted type-aware data binding. (If you need lots of sample data you are better off using the DesignData extension)

CreateList (Defaults to false)

If true the DesignInstance will actually be created as a list of the specified Type. From what I can tell this is the only way to specify your design instance as a collection (I briefly tried to pass a generic list as the Type parameter but could not get it to work, hence why I expect this parameter exists).

Note that DesignInstance and DesignData are mutually exclusive mark-up extensions. You cannot set both DesignInstance and DesignData extensions when setting your DataContext.

In general the main use of DesignInstance extension is to allow you to use the data binding tools within Blend and Visual Studio. It can give you some sample data if you set properties in your constructor.

To be able to design effectively you really need plentiful and varied sample data. For the best design experience you should consider using the DesignData extension – the drawbacks being the slightly increased upfront cost of creating a XAML file containing your sample data, and the increased complexity of refactoring the XAML whenever your classes change.

An Auto-Centering Canvas for WPF

If you try using WPF Canvas to plot objects in a 2d “scene”, you will hit a small problem. The Left and Top dependency properties dictate where the top left corner of your element is positioned. Not so good if you want to plot your elements at or over the coordinate position.

Here is a simple way to extend the Canvas such that Top and Left position your elements around their center.

    public class CenteringCanvas : Canvas
    {
        protected override Size ArrangeOverride(Size arrangeSize)
        {
            foreach (UIElement element in base.InternalChildren)
            {
                if (element == null)
                {
                    continue;
                }
                double x = 0.0;
                double y = 0.0;
                double left = GetLeft(element);
                if (!double.IsNaN(left))
                {
                    x = left - element.DesiredSize.Width/2;
                }
                
                double top = GetTop(element);
                if (!double.IsNaN(top))
                {
                    y = top - element.DesiredSize.Height/2;
                }
                
                element.Arrange(new Rect(new Point(x, y), element.DesiredSize));
            }
            return arrangeSize;
        }
    }

Of course don’t expect this code to work if you try setting the Right or Bottom properties.

Microsoft Surface development goes public

At PDC this year Microsoft announced that Surface development resources were going to be made publicly available. (Up until now it had been an “invitation only” system).

This is great news and means anyone can build or prototype Surface applications. The Surface SDK comes with a very good simulator. A table is still needed to evaluate performance and usability, but you can go a long way with just the simulator.

Microsoft Surface Simulator

Here is a copy & paste from a recent mail-out:

  • Surface Web site (http://www.surface.com)
  • MSDN (http://msdn.microsoft.com)
  • TechNet (http://technet.microsoft.com)
  • Microsoft Download center (http://www.microsoft.com/downloads)
    • A new product category for all Surface downloads, including the Surface SDK Workstation Edition, applications, documentation, and any software fixes.
  • Microsoft Support (http://support.microsoft.com)
  • WCF Data Services vs WCF RIA Services

    I’ve been having trouble finding a straight forward comparison of these two technologies. On the surface they appear to be solving similar, if not the same problems.

    Make no mistake – the message coming from Microsoft is not clear or consistent – which probably explains the confusion. I suspect the relationship between these two products is still being “discovered” by MS.

    Anyway, in this bliki I will try to sum up my findings as to the differences between them as I figure them out.

    WCF (ADO.NET) Data Services

    WCF (.NET) RIA Services

    Expose data model as RESTful web service Prescriptive approach to n-tier app development
    Cross platform interoperation as a goal
    – “Unlock data silos”
    – Out-of-box support from future MS products such as SQL2008 R2, Azure, Excel 2010, SharePoint 2010
    Designed specifically for end-to-end Silverlight & ASP.NET solutions
    – Some technology proprietary to Silverlight (no WPF support)
    – Use ASP.NET Authentication/Roles across SL and ASP.NET
    – ASP.NET/AJAX can also access service layer
    Loosely coupled clients and servers Client & server are designed and deployed together
    Service layer exposes “raw” data sources Opportunity to easily add business logic into service layer
    – Encourage “domain” concepts
    – Strong validation framework
    – Offline / Sync enabled
    Service can be consumed from .NET, Silverlight, AJAX, PHP and Java (libraries available) Service can be consumed easily from SL, AJAX, WebForms
    Service’s data source must:
    – Expose at least one IQueryable property
    – Implement IUpdateable if you desire updates
    Service exposes domain objects via convention:
    – IQueryable GetX
    – UpdateX/InsertX/DeleteX
    No design time experience yet (??) Design time experience with data sources, drag drop etc
    – OData for all clients
    – Within OData, multiple formats supported (JSON, XML etc)
    – SOAP (binary) for SL clients
    – JSON for AJAX clients
    – SOAP (XML) for other clients
    Discoverable (?) Non-discoverable
    Hosted as WCF Service (.svc) Old version hosted in custom web handler (.axd).
    New version is WCF service.
    Standardized on OData protocol Will “support” OData
    More mature – public for at least 2 years, formerly “Project Astoria” Less mature – public for 6 months


    Common features

    • Based on WCF
    • Use a RESTful architecture
    • Can be used to expose any data source (sql, xml, poco/objects etc.)
    • Client side libraries provide ability to query using LINQ

    General

    • Currently they do not share much (any?) technology / code
    • RIA Services is not based on top of Data Services
    • RIA Services & Data Services will “align”
    • OData eventually pushed down into WCF stack

    Your opinions are welcome!

    References

    http://blogs.msdn.com/brada/archive/2009/03/19/what-is-net-ria-services.aspx

    http://mschannel9.vo.msecnd.net/o9/mix/09/pptx/t36f.pptx

    http://blogs.msdn.com/endpoint/archive/2009/11/18/the-wcf-services-ecosystem.aspx

    http://www.douglaspurdy.com/2009/11/20/on-odata-open-data-protocol/

    http://msdn.microsoft.com/en-us/data/ee844254.aspx

    http://blogs.msdn.com/saurabh/archive/2009/11/23/understanding-the-wcf-in-wcf-ria-services.aspx

    Expression Blend 3 Help: The missing DelegateCommand class

    Some tutorials from the Expression Blend 3 help docs (e.g. “Try it: Display data from a sample SQL database”, which is based on these two articles) require a class that is present in the ColorSwatch WPF sample. The problem is that someone appears to have forgotten to bundle the ColorSwatch sample with the Blend 3!

    To save you the hassle of trying to track down the DelegateCommand class here it is:

    using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using System.Windows.Input; 
     
    namespace ColorSwatch 
    { 
        public sealed class DelegateCommand : ICommand 
        { 
            public delegate void SimpleEventHandler(); 
     
            private SimpleEventHandler handler; 
     
            private bool isEnabled = true; 
     
            public DelegateCommand(SimpleEventHandler handler) 
            { 
                this.handler = handler; 
            } 
     
            #region ICommand implementation 
     
            void ICommand.Execute(object arg) 
            { 
                this.handler(); 
            } 
     
            bool ICommand.CanExecute(object arg) 
            { 
                return this.IsEnabled; 
            } 
     
            public event EventHandler CanExecuteChanged; 
     
            #endregion 
     
            public bool IsEnabled 
            { 
                get { return this.isEnabled; } 
                set 
                { 
                    this.isEnabled = value; 
                    this.OnCanExecuteChanged(); 
                } 
            } 
     
            private void OnCanExecuteChanged() 
            { 
                if (this.CanExecuteChanged != null) 
                { 
                    this.CanExecuteChanged(this, EventArgs.Empty); 
                } 
            } 
        } 
    }