user380719
user380719

Reputation: 9903

How can I bind against the Index of a ListBoxItem

I'd like to bind the z index of list box items to their index.

Ideally, we would have

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="Panel.ZIndex"
            Value="{Binding RelativeSource={RelativeSource Self}, Path=-Index}" />
    <!-- ... -->

However, the list box item does not have an index property.

I can think of a number of crazy solutions but nothing simple and elegant.

Any taker?

Upvotes: 0

Views: 2727

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292445

There is no Index property, but anyway "-Index" wouldn't be a valid path... you would still need a converter to negate the value. So what you can do is create a converter that retrieves the index from the ItemContainerGenerator

public class ItemContainerToZIndexConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var itemContainer = (DependencyObject)value;
        var itemsControl = FindAncestor<ItemsControl>(itemContainer);
        int index = itemsControl.ItemContainerGenerator.IndexFromContainer(itemContainer);
        return -index;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    public static T FindAncestor<T>(this DependencyObject obj) where T : DependencyObject
    {
        var tmp = VisualTreeHelper.GetParent(obj);
        while (tmp != null && !(tmp is T))
        {
            tmp = VisualTreeHelper.GetParent(tmp);
        }
        return (T)tmp;
    }
}


<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="Panel.ZIndex"
            Value="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource zIndexConverter}}" />
    <!-- ... -->

Upvotes: 3

Related Questions