Hmmm, seems you can't bind ZIndex. Not sure why that is. How about something like this (setting it manually):
<Window
x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="MyMainWindow">
<StackPanel>
<Canvas Width="300" Height="300" Background="Blue">
<Ellipse Name="green" Fill="Green" Width="200" Height="100" />
<Ellipse Name="red" Fill="Red" Width="200" Height="100" Canvas.Left="100" Canvas.Top="50" />
</Canvas>
<Button Click="Button_Click">Swap</Button>
</StackPanel>
</Window>
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
RedIndex = 1;
GreenIndex = 2;
}
private int redIndex;
private int greenIndex;
public int RedIndex
{
get
{
return redIndex;
}
set
{
if (redIndex != value)
{
redIndex = value;
Canvas.SetZIndex(red, redIndex);
}
}
}
public int GreenIndex
{
get
{
return greenIndex;
}
set
{
if (greenIndex != value)
{
greenIndex = value;
Canvas.SetZIndex(green, greenIndex);
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
int index = RedIndex;
RedIndex = GreenIndex;
GreenIndex = index;
}
}
}
You'd have to modify that so you can get to the dynamically generated item containers.