wpf - 将 ItemsControl 与 ItemsSource 绑定(bind)到数组索引

标签 wpf arrays binding indexing itemscontrol

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

客户 1:

姓名:xxxx

年龄:888

客户 2:

姓名:yyy

年龄:7777

最佳答案

最简单的方法是在类中添加一个 Index 属性 ;-) 否则可以使用 MultiValueConverter 来完成:

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

感谢this question我找到了另一种声明 MultiBinding 的方法:

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

关于wpf - 将 ItemsControl 与 ItemsSource 绑定(bind)到数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1810180/

相关文章:

C#.Net AsSpan().Fill() byte[] vs Int32[] vs float[] 速度差异取决于数组长度?

javascript - 客户端 JavaScript 的 sprintf 等价物

wpf - 如何在WPF/MVVM中将ControlTemplate(用于复选框)设置为只读?

c# - 安装 ClickOnce 应用程序时出现问题

c# - 跳过 Facebook 登录窗口

WPF 绑定(bind)到 ItemsControl 内的 ElementName

c# - ASP.NET Repeater : Is it possible to display on the first item one element, 但不适用于其余项目?

c# - MVC ViewModel EntityFrameWork ICollection,IEnumerable 虚拟类

javascript - d3 js 圆形条形图,如何传递对象而不是 .csv 文件?

javascript - 查找数组中最长的单词/字符串