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"/>

posted @ 2020-04-16 16:35  寒夜美美  阅读(1039)  评论(0编辑  收藏  举报