WPF:XAML中的绑定列表 - 项目如何知道其在列表中的位置? [英] WPF: Binding lists in XAML- how can an item know its position in the list?

查看:357
本文介绍了WPF:XAML中的绑定列表 - 项目如何知道其在列表中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下XAML代码,使用ListControl类似行为:

Given the following XAML code that with a ListControl like behavior:

    <StackPanel>
        <ItemsControl Name="_listbox" ItemsSource="{Binding ElementName=_userControl, Path=DataContext}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <DockPanel>
                         ...
                    </DockPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>

由于列表可以很长(100-200项),项目看起来相似,我想如果每个项目都会在列表中显示他们的位置,则在滚动期间用户将有所帮助。 模板中的项目如何知道自己在列表中的位置?

Since the list can be long (100-200 items), and the items look similar, I think it would helpful for the user during scrolling if every item would display their position in the list. How could an item in the template know its own position in the list?

推荐答案

黑客解决方案我们可以使用Value Conversion with DataBinding。所以第一步是声明我们的ValueConvertor:

Here is a hack solution. We can use Value Conversion with DataBinding. So the first step is to declare our ValueConvertor:

public class ListItemToPositionConverter : IValueConverter
    {
        #region Implementation of IValueConverter

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var item = value as ListBoxItem;
            if (item != null)
            {
                var lb = FindAncestor<ListBox>(item);
                if (lb != null)
                {
                    var index = lb.Items.IndexOf(item.Content);
                    return index;
                }
            }
            return null;
        }            

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

声明无论你想要这个静态方法来获取ListBox的父类:

Declare wherever you want this static method in order to obtain ListBox parent:

public static T FindAncestor<T>(DependencyObject from) where T : class
        {
            if (from == null)
                return null;

            var candidate = from as T;
            return candidate ?? FindAncestor<T>(VisualTreeHelper.GetParent(from));
        }

然后在ListBox.Resources中声明我们的转换器如下:

Then in ListBox.Resources declare our convertor as follows:

<ListBox.Resources>
                <YourNamespace:ListItemToPositionConverter x:Key="listItemToPositionConverter"/>
            </ListBox.Resources>

最后 - DataTemplate:

And finally - DataTemplate:

<ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Converter={StaticResource listItemToPositionConverter}}"/>
                        <Label Content="{Binding Path=DisplayName}"></Label>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>

注意:在此示例中,项目将从0开始计数(零),您可以通过在结果中添加1来转换方法。

Note: in this example the items will be numerated starting with 0 (zero), you can change it in Convert method by adding 1 to result.

希望这有帮助...

这篇关于WPF:XAML中的绑定列表 - 项目如何知道其在列表中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆