XAML for C# developers

XAML mark-up describes a DOM in which the nodes are instances of classes, some of which are your own code, some of which are .NET framework code. Like your own code, classes must be referenced before they can be instantiation. A reference is written as an attribute of a page or user control and looks like this.

xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Ria"

The name riaControls in the above sample can then be used in XAML as an alias for the CLR namespace System.Windows.Controls mentioned in its definition. Once you’ve declared the reference, you can use the alias in XAML to create an instance of a class in that namespace, like this.

<riaControls:DomainDataSource AutoLoad="True" Height="0" LoadedData="employeeGroupDomainDataSource_LoadedData" Name="employeeGroupDomainDataSource" QueryName="GetEmployeeGroupsQuery" Width="0">
  <riaControls:DomainDataSource.DomainContext>
    <my1:AtomDomainContext />
  </riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>

That example is roughly equivalent to

System.Windows.Controls.DomainDataSource employeeGroupDomainDataSource =
  new System.Windows.Controls.DomainDataSource();

employeeGroupDomainDataSource.QueryName = "GetEmployeeGroupsQuery";

employeeGroupDomainDataSource.Width = 0;

employeeGroupDomainDataSource.LoadedData += employeeGroupDomainDataSource_LoadedData;

The biggest hassle with XAML is scope.  As far as I’ve been able to determine, declarations that are subordinated in XAML are likewise subordinated in scope, so the DomainContext created in the above sample is effectively declared as a member of the DomainDataSource and an instance of the DomainContext is created and assigned in the constructor of the DomainDataSource.

A quirk of binding is correspondence of scope. If you bind a DataGrid to a DomainDataSource then the columns of that DDS are directly in scope for the immediate children of the DataGrid. I’m still trying to figure out exactly what’s going on here.

Published 12-15-2009 12:59 by peterw