0

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.

Update: This is the ItemsContainerStyle, defined as a Window Resource

<Style x:Key="MediaContainerStyle" TargetType="ListBoxItem">
    <Setter Property="Canvas.Left" Value="{Binding MediaPosition.X,UpdateSourceTrigger=PropertyChanged}"/>
    <Setter Property="Canvas.Top" Value="{Binding MediaPosition.Y,UpdateSourceTrigger=PropertyChanged}"/>
    <Setter Property="Panel.ZIndex" Value="{Binding ZIndex,UpdateSourceTrigger=PropertyChanged}"/>
</Style>

and the template for the item

<DataTemplate x:Key="MediaDataTemplate">
    <views:MediaItemView MouseDown="OnMediaItemMouseDown"
                         MouseMove="OnMediaItemMouseMove"/>
</DataTemplate>

where MediaItemView is a user control.

In the code behind, I do

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);
    }
}

where vm is an instance of my underlying viewmodel, containing a Double property ZIndex

  • 1
    Hard to tell without seeing your code. However, make sure you set the ZIndex in the ItemContainerStyle, not in the ItemTemplate. – Clemens May 29 '16 at 10:39
  • I tried that. I bound Panel.ZIndex of the ItemContainerStyle to a property in my underlying viewmodel, and changed that on mouse down, but it doesn't work. What code would you like to see? – Geysser Sdz May 29 '16 at 10:44
  • "What code would you like to see?" All relevant, i.e. the ListBox's XAML and the corresponding parts of the view model. – Clemens May 29 '16 at 11:07
  • So show the ZIndex property in MediaViewModel. Sure you're releasing the PropertyChanged event? – JeffRSon May 29 '16 at 11:30
  • Yes, I do `public Double ZIndex { get { return _ZIndex; } set { if (_ZIndex != value) { _ZIndex = value; OnPropertyChanged("ZIndex"); } } }` – Geysser Sdz May 29 '16 at 11:36
  • 1
    Note that all the `UpdateSourceTrigger=PropertyChanged` setting on the Bindings in the Style Setters are redundant and have no effect. `UpdateSourceTrigger=PropertyChanged` only makes sense in two-way bindings. – Clemens May 29 '16 at 14:56

1 Answers1

1

And, of course, it was right before my eyes! The answer, (taken from Modify ZIndex of an Items in an ItemsControl) was to add a trigger to my ItemContainerStyle (which has a ListBoxItem as TargetType) for IsSelected property. So...

<Style.Triggers>
    <Trigger Property="IsSelected" Value="True">
        <Setter Property="Panel.ZIndex" Value="99"/>
    </Trigger>
</Style.Triggers>

will do!

Community
  • 1
  • 1