Skip to content

NameReferenceExtension – MarkupExtension to do name references

July 24, 2007

Several people have been asking for the ability to do name references to set a property on one object to another object somewhere in the Xaml document.

Forum Posts:

1) Sample of MarkupExtension taking in a FrameworkElement?

2) Create a markup extension: How to get an element by name and other problems…

Challenge

We plan to enable this in the XamlLanguage natively in the future…until then there are ways to make it work in .Net 3.0/3.5 with our XamlReader.

I built two generic CLR objects: Root and Node that I wanted to use like this:

<?xml version="1.0" encoding="utf-8" ?>
<Root xmlns="clr-namespace:XamlForumMEs;assembly=XamlForumMEs">

  <Node Name="Node1" />
  <Node Name="Node2" Friend="{NameReference Node1}" />
  <Node Name="Node3" Friend="{NameReference Node4}" />
  <Node Name="Node4" />
  <Node Name="Node5" />
  <Node Name="Node6" />

  <Node />
</Root>

Implementing the first NameReference isn’t too hard, since it is a backwards reference.

Implementing a NameReference so that it can do the forward reference (Node3 making Node4 a friend) is more challenging.

Brief Details of Sample

I made Root implement:

  • INameScope – so that each Name property setting would be registered with the root.
  • ISupportInitialize – so that Root would call BeginInit and EndInit at the start and end of parsing the tag.

Each time a NameReference was asked to Provide a value, I cached a DeferredFriend request in the Root.

At EndInit of Root, I set all DeferredFriends.

Not extremely pretty…but it may help some folks until we make this cleaner…

Download Link

http://robrelyea.com/demos/NameReference/NameReference.zip (a project that was built with VS2008 Beta2…if you are using VS2005, you need to manually copy the files into a new project)

From → XAML (non-UI)

7 Comments
  1. Lloyd permalink

    I did some more investigation.NameScope already has static methods which let me do most of the thing provided I work with DependencyObject.
     
    But one problem remains though.If I try to use my extension in a template I have hard time initializing it at the appropriate time. (that is when everything has been binded correctly).With reflector\’s help I noticed the way Binding use the Expression class. Would be nice if we could do the same with our own code.
    For now I limit my extension to work with FrameworkElement and use the Loaded event to do all the real work of calculating the values (well that\’s the idea so far, as I work on it sporadically in my own time)

  2. Lloyd permalink

    I\’m trying to go on by assuming I work with FrameworkElement.
    However I have problem with this simple XAML:
    <Rectangle Stroke="#FF353535"   RadiusX="{gl:Expression \’6\’}"   RadiusY="6"   Width="{TemplateBinding Width}"   Height="{TemplateBinding Height}"> when my extension is called it is passed a System.Windows.SharedDp as its target.consequently I cannot really register the Loaded event, lookup names, etc…any tip/workaround?!

  3. Rob permalink

    About to hit the road for vacation, so may not be able to help for a while.
    The 2nd comment you made with the markup example…was that inside a ControlTemplate, I assume? 

  4. Lloyd permalink

    Now that you mention it, indeed it is in a ControlTemplate.
    I was wondering why I was getting this private SharedDp class in some case an in other. Mhh, thinking about it…. 

  5. Andres permalink

    Hey Rob,
     
    I am beginning my investigation into using extensions and ran across your code. Very nice work.
     
    I tried modifying the usage of the root class in the form and tried to bind it to a listbox to display the nodes.
     
    <Window    x:Class="XamlForumMEs.Window1"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:self="clr-namespace:XamlForumMEs;assembly=XamlForumMEs"    Title="Window1" Height="300" Width="300">    <Window.Resources>        <self:Root x:Key="rootObject">            <self:Node Name="Node1" />            <self:Node Name="Node2" Friend="{self:NameReferenceExtension Node1}" />            <self:Node Name="Node3" Friend="{self:NameReferenceExtension Node4}" />            <self:Node Name="Node4" />            <self:Node Name="Node5" />            <self:Node Name="Node6" />            <self:Node />        </self:Root>    </Window.Resources>    <Grid>        <ListBox             x:Name="listRoot"            DataContext="{StaticResource rootObject}"            ItemsSource="{Binding Children}"            >            <ListBox.ItemTemplate>                <DataTemplate>                    <StackPanel>                        <TextBlock Text="{Binding Name}"/>                        <TextBlock xml:space="preserve" Text=" : "/>                        <TextBlock Text="{Binding Friend}"/>                    </StackPanel>                </DataTemplate>            </ListBox.ItemTemplate>        </ListBox>    </Grid></Window>
     
    But I have been running into an error:
     
    The tag \’Root\’ does not exist in XML namespace \’clr-namespace:XamlForumMEs;assembly=XamlForumMEs\’. Line 8 Position 10.
     
    Is there sometrhing wrong with my usage?
     
    Thanks in advance,
     
    Andres

  6. Unknown permalink

    Welcome to enter (wow gold) and (wow power leveling) trading site, (Rolex) are cheap, (World of Warcraft gold) credibility Very good! Quickly into the next single! Key words directly to the website click on transactions! -144657701846990

  7. Unknown permalink

    Amberdigital Branch,Southern Stars Enterprises Co is specializing in the development and manufacturing of advertisement screens, LCD digital signage and LCD signages. Established in 1996, we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

    amberdigital Contact Us
    Southern Stars Enterprises Co (Hong Kong Office)
    Add:3 Fl, No.2, Lane 2, Kam Tsin Tsuen, Sheung Shui, Hong Kong
    Tel:+852 2681 4099
    Fax:+852 2681 4586
    Southern Stars Enterprises Co (Shenzhen Office)
    Add:DE, 16/F, Building 2, Nanguo Tower, Sungang Road, Shenzhen, China
    Tel:+86 755 2592 9100
    Fax:+86 755 2592 7171
    E-mail:sstar@netvigator.com
    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[aiebh

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: