WPF Listview加一列行号
public class ListviewIndexConvert : IValueConverter { public object Convert(object value, Type TargetType, object parameter, CultureInfo culture) { ListViewItem item = (ListViewItem)value; ListView listView = ItemsControl.ItemsControlFromItemContainer(item) as ListView; int index = listView.ItemContainerGenerator.IndexFromContainer(item) + 1; string num = string.Empty; if (index.ToString().Length == 1) { num = "0" + index; } else { num = index.ToString(); } return num; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
引用:
<local:ListviewIndexConvert x:Key="IndexConvert"/>
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListViewItem}},Converter={StaticResource IndexConvert}}" FontSize="13" Foreground="#FFFFFF" VerticalAlignment="Center" HorizontalAlignment="Center"/>