开发者

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}开发者_如何学JAVA">
    <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?


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}}" />
    <!-- ... -->
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜