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