Wpf 将ItemsControl与ItemsSource绑定到数组索引

Wpf 将ItemsControl与ItemsSource绑定到数组索引,wpf,arrays,binding,indexing,itemscontrol,Wpf,Arrays,Binding,Indexing,Itemscontrol,我想使用ItemsSource和DataTemplate将ItemsControl绑定到对象数组。 我想显示每个项目的索引。 像 客户1: 姓名:xxxx 年龄:888 客户2: 姓名:yyy Age:7777执行此操作的最简单方法是向类中添加索引属性;-)否则,可使用多值转换器进行: <Window x:Class="IndexSpike.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentati

我想使用ItemsSource和DataTemplate将ItemsControl绑定到对象数组。 我想显示每个项目的索引。 像

客户1:

姓名:xxxx

年龄:888

客户2:

姓名:yyy


Age:7777执行此操作的最简单方法是向类中添加索引属性;-)否则,可使用多值转换器进行:

<Window x:Class="IndexSpike.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300"
    xmlns:Converters="clr-namespace:IndexSpike"
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    Name="Root"
    >
    <Window.Resources>
        <Converters:GetIndex x:Key="GetIndexConverter"/>
    </Window.Resources>
    <StackPanel>
        <ItemsControl ItemsSource="{Binding Persons}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Margin="0,5,0,0">
                        <TextBlock.Text>
                            <MultiBinding Converter="{StaticResource
                                            GetIndexConverter}"
                                            StringFormat="Index: {0}">
                                <Binding Path="."/>
                                <Binding ElementName="Root" Path="Persons"/>
                            </MultiBinding>
                        </TextBlock.Text>
                        </TextBlock>
                        <TextBlock Text="{Binding Name, StringFormat='Name: {0}'}"/>
                        <TextBlock Text="{Binding Age, StringFormat='Age {0}'}"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</Window>

using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace IndexSpike
{
    public partial class Window1 : Window
    {
        public ObservableCollection<Person> Persons { get; set; }

        public Window1()
        {
            Persons=new ObservableCollection<Person>
                        {
                            new Person("Me",20),
                            new Person("You",30)
                        };
            InitializeComponent();
            DataContext = this;
        }
    }

    public class Person
    {
        public Person(string name,int age)
        {
            Name = name;
            Age=age;
        }

        public string Name { get; set; }
        public int Age { get; set; }
    }

    public class GetIndex:IMultiValueConverter
    {
        #region Implementation of IMultiValueConverter

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var persons = (ObservableCollection<Person>) values[1];
            var person = (Person) values[0];
            return persons.IndexOf(person);
        }

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

        #endregion
    }
}

使用制度;
使用System.Collections.ObjectModel;
利用制度全球化;
使用System.Windows;
使用System.Windows.Data;
名称空间索引
{
公共部分类Window1:Window
{
公共可观察集合人员{get;set;}
公共窗口1()
{
人员=新观察到的集合
{
新人(“我”,20岁),
新人(“你”,30岁)
};
初始化组件();
DataContext=this;
}
}
公共阶层人士
{
公众人物(字符串名称,整数年龄)
{
名称=名称;
年龄=年龄;
}
公共字符串名称{get;set;}
公共整数{get;set;}
}
公共类GetIndex:IMultiValueConverter
{
#IMultiValueConverter的区域实现
公共对象转换(对象[]值,类型targetType,对象参数,CultureInfo区域性)
{
var persons=(可观测集合)值[1];
var person=(person)值[0];
返回人员。索引of(人员);
}
公共对象[]转换回(对象值,类型[]目标类型,对象参数,CultureInfo区域性)
{
抛出新的NotImplementedException();
}
#端区
}
}
多亏了,我找到了另一种声明多重绑定的方法:

<MultiBinding Converter="{StaticResource GetIndexConverter}"
                                          StringFormat="Index: {0}">
     <Binding Path="."/>
     <Binding RelativeSource="{RelativeSource FindAncestor,
                                  AncestorType={x:Type ItemsControl}}" 
                                  Path="DataContext.Persons"/>
</MultiBinding>