All posts by Jeremy

WPF internals part 3 : how Z-Index works ?

Almost 1 year ago, I started a series of blog posts entitled “WPF Internals”. I though I’d have more time to write entries on this subject but I wrote only 2 subjects:

Today, I’m ready to share with you part 3: how the Z-Index functionality works ? As for the other article of this series of blog post, the information I’m sharing here is based on my personal understanding of how stuff works. It might be wrong on some points !

The Z-Index is a property which can be set in order to control the order of appearance of your control:

In WPF it’s the Panel type which defines an attached property called Zindex:

public static readonly DependencyProperty ZIndexProperty = DependencyProperty.RegisterAttached(
	"ZIndex", 
	typeof(int), 
	typeof(Panel), 
	new FrameworkPropertyMetadata(0, new PropertyChangedCallback(Panel.OnZIndexPropertyChanged)));

which register a PropertyChangedCallback to be notified whenever its value changes. This method finally calls another method:

internal void InvalidateZState()
{
    if (!this.IsZStateDirty && (this._uiElementCollection != null))
    {
        base.InvalidateZOrder();
    }
    this.IsZStateDirty = true;
}

As you can see, the value of the IsZStateDirty property is set to true. We’ll soon see when this value is used. The InvalidateZOrder() is actually found in the Visual class. Here is a brief reminder of the core WPF types:

So, in the Visual type we have the InvalidateZOrder() method:

[FriendAccessAllowed]
internal void InvalidateZOrder()
{
    if (this.VisualChildrenCount != 0)
    {
        this.SetFlags(true, VisualFlags.NodeRequiresNewRealization);
        /* … */
    }
} 

Which, as you can see, update the value of an enumeration (VisualFlags). Then the chain of method call stops here. The next interesting steps is when the GetVisualChild method (from the FrameworkElement type) gets called in the Panel type:

if (this.IsZStateDirty)
{
    this.RecomputeZState();
}

The RecomputeZState is a private method of the Panel class. The goal of this method is to update an array of int which is used as a lookup-table in order to convert the visual elements from their logical positions to their visual positions. At the end of this method, which is by the way highly-optimized with stuff like this

int z = _elements[i] != null
? (int)z = _elements[i].GetValue(ZIndexProperty)
: zIndexDefaultValue;

stableKeyValues.Add(((Int64)z << 32) + i);
lutRequired |= z < prevZ;
prevZ = z;

isDiverse |= (z != consonant);

the IZStateDirty value is set to false, and the zLut (z-order look up table, an int[]) is up-to-date. The the GetVisualChild method simply use the lookup table to convert logical position to visual position

int num = (this._zLut != null) ? this._zLut[index] : index;
return this._uiElementCollection[num];

Summary:

  • The Z-Index functionality of WPF is implemented using an attached property
  • This attached property is defined in the Panel type
  • When the value of the attached property changes, a flag is set at the Visual level and at the Panel level
  • When the GetVisualChild methods gets called on the Panel, the dirty status of the Z-Index is check
  • If necessary, a lookup-table is computed to convert logical position (in the Children collection) from visual position
  • Changing the ZIndex property of a child object does not change its position within the collection. The ordering within the collection remains the same

Tap N Match a cool WP7 game ! Vote on Facebook !

Today I need help from all my readers across the globe ! Please vote for our TapNMatch WP7 game here

Microsoft France is organizing since a couple of months a Windows Phone 7 contest in order to create cool applications. Applications are demonstrated using a short video which is posted on Facebook. The goal is to have the maximum number of “Like” in order to be selected for the final.10 applications are going to be selected, and on October 7th, each selected developers will have to present very quickly (2min) its application in front of an amazing jury made of Steve Ballmer and CEOs of french companies.

A coworker and I have been working on a Windows Phone 7 game called Tap N Match created using Silverlight. The goal of the game is to associate “targets” which have a number either by creating pairs (5;5) or sequences (1;2). Here is quick description of the game:

  • best scores are recorded in the cloud using Windows Azure
  • use of the Bing APIs in order to determine the country of the player when posting a new score
  • use of the Pivot control

Here is the presentation of our application on YouTube:

In order to vote for our application:

  1. Go to : http://www.facebook.com/Developpeurs.net
  2. “Like” this page
  3. Go to the video of my game : http://www.facebook.com/Developpeurs.net#!/video/video.php?v=434150728926
  4. “Like” my video 🙂

Thank you very much for your help !

MSDN Ultimate Subscription Giveaway

IMG_0833 As a Microsoft MVP for this year, I got 3 MSDN Ultimate Subscriptions to share with friends and co-workers. I already gave 2 of them to co-workers and I’d like to offer the last one to one of my reader !

The “official” pricing for the MSDN Ultimate Subscription is $11,899. The subscription is valid one year and is not restricted to US only. The MSDN Ultimate has the following items (among many others – you can see the detailed list here):

  • Windows Azure Platform
  • Visual Studio 2010 Ultimate
  • Visual Studio TFS 2010
  • Expression Studio Ultimate
  • Windows 7, Windows Server 2008 R2 and SQL Server 2008
  • Microsoft Office Professional Plus 2010, Project Professional 2010, Visio Premium 2010

In order to take your chance to get this subscription:

  • let a comment on this blog post
  • explain what you would like to do with the subscription
  • share you blog address, community website or whatever to show how you’re involved in the .Net community

Contest is now over. The winner of the MSDN Ultimate License is Mike Strobel. Thank you all for letting a comment, I which I had more subscriptions to giveaway…