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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM