title: ZIndex on ObservableCollection bound ListBox
body: <p>I have a ListBox bound to an ObservableCollection, with a canvas as an ItemsPanel. Everything works as expected - I've implemented dragging of the items succesfuly - but the problem is that I can't set the ZIndex of the clicked item. Debuging shows that all the items have a ZIndex of 0, which looks strange to me. What I want is to bring the item to front when clicked and send to back when released. Could someone give me any ideas? Please feel free to ask for any code that might be useful.</p>
  
  <p>Update: This is the ItemsContainerStyle, defined as a Window Resource</p>
  
  <pre><code>&lt;Style x:Key="MediaContainerStyle" TargetType="ListBoxItem"&gt;
      &lt;Setter Property="Canvas.Left" Value="{Binding MediaPosition.X,UpdateSourceTrigger=PropertyChanged}"/&gt;
      &lt;Setter Property="Canvas.Top" Value="{Binding MediaPosition.Y,UpdateSourceTrigger=PropertyChanged}"/&gt;
      &lt;Setter Property="Panel.ZIndex" Value="{Binding ZIndex,UpdateSourceTrigger=PropertyChanged}"/&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>and the template for the item</p>
  
  <pre><code>&lt;DataTemplate x:Key="MediaDataTemplate"&gt;
      &lt;views:MediaItemView MouseDown="OnMediaItemMouseDown"
                           MouseMove="OnMediaItemMouseMove"/&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>where MediaItemView is a user control.</p>
  
  <p>In the code behind, I do</p>
  
  <pre><code>void OnMediaItemMouseDown(Object sender, MouseButtonEventArgs e)
  {
      if (e.LeftButton == Pressed)
      {
          FrameworkElement feItem = sender as FrameworkElement;
          MediaViewModel vmItem = feItem.DataContext as MediaViewModel;
          vmItem.ZIndex = vm.MainMedia.Count;
          // Keep the click point
          pClick = e.GetPosition(feItem);
      }
  }
  </code></pre>
  
  <p>where vm is an instance of my underlying viewmodel, containing a Double property ZIndex</p>