Welcome to MSDN Blogs Sign in | Join | Help

Lester's WPF blog


Simple, easy & beautiful

News

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

 

Posted: Friday, November 13, 2009 3:07 PM by llester
Attachment(s): NameReferences.zip

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker