Item2IndexConverter.cs 2.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace FLY.ControlLibrary.Converter
{
    /// <summary>
    /// 列表转序号
    /// </summary>
    [ValueConversion(typeof(object), typeof(int))]
    public class Item2IndexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            int result = -1;
            if (value != null)
            {

                var collectionViewSource = parameter as CollectionViewSource;
                if (collectionViewSource != null)
                {
                    var cv = (CollectionView)collectionViewSource.View;
                    //在 设计模式中, View为null, 所以下面必须判断
                    if (cv != null)
                    {
                        result = cv.IndexOf(value);
                    }
                }
            }
            return result >= 0 ? result : System.Windows.DependencyProperty.UnsetValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    /// <summary>
    /// 列表转Number 从1开始
    /// </summary>
    [ValueConversion(typeof(object), typeof(int))]
    public class Item2NoConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            int result = -1;
            if (value != null)
            {

                var collectionViewSource = parameter as CollectionViewSource;
                if (collectionViewSource != null)
                {
                    var cv = (CollectionView)collectionViewSource.View;
                    //在 设计模式中, View为null, 所以下面必须判断
                    if (cv != null)
                    {
                        result = cv.IndexOf(value);
                    }
                }
            }
            return result >= 0 ? result + 1 : System.Windows.DependencyProperty.UnsetValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
75
}