WPF Listview加一列行号

时间:2020-04-16
本文章向大家介绍WPF Listview加一列行号,主要包括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"/>

原文地址:https://www.cnblogs.com/candyzhmm/p/12713803.html