Partilhar via


XAML 2009 Features: Named Object References

This is part of a series on New WPF\XAML Features

In previous releases, if you needed to reference a named object, your XAML would look something like

        <Label Target='{Binding ElementName=firstNameBox}' >_Target</Label>

        <TextBox Name='firstNameBox'>Uses Binding</TextBox>

 

In the current release (.NET 4), we introduced a built in markup extension x:Reference. This would enable referencing a named object. The above XAML can now be written as

        <Label Target= '{x:Reference secondNameBox}'>_Second Target</Label>

Or

        <Label Target= 'thirdNameBox' >_Third Target</Label>

 

The references look both forward as well as backwards

Another possibility working with named objects is to create a markupextension that calls into IXamlNameResolver to resolve a name. One scenario is having method calls in XAML like the below. We covered this is a previous post written by Shree.

<School Topper="{Call students.GetTopper}" >

    <School.Class>

        <Students x:Name="students">

            <Student Name="Audrey" Marks="90" />

            <Student Name="Bill" Marks="95" />

    </Students>

    </School.Class>

    <!-- This is backward reference -->

    <!--<School.Topper>

        <Call Expression="students.GetTopper" />

    </School.Topper>-->

</School>

 

 

A simple project showing the usage of x:Reference is attached

 

**

Note that XAML 2009 features work only for loose XAML in WPF.

Share this post

 

NameReferences.zip