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
42
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();
}
}
}