基本上我在这里使用了发布的转换器,只是将数据类型更改为泛型。如何在 XAML 中使用通用转换器?
public class ReverseListConverter<T> : MarkupExtension, IValueConverter
{
public ReverseListConverter()
{
}
private ObservableCollection<T> _reversedList;
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
_reversedList = new ObservableCollection<T>();
var data = (ObservableCollection<T>)value;
for (var i = data.Count - 1; i >= 0; i--)
_reversedList.Add(data[i]);
data.CollectionChanged += DataCollectionChanged;
return _reversedList;
}
void DataCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
var data = (ObservableCollection<T>)sender;
_reversedList.Clear();
for (var i = data.Count - 1; i >= 0; i--)
_reversedList.Add(data[i]);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
在转换器通用之前,我刚刚使用
ItemsSource="{Binding Mails, Converter={tools:ReverseListConverter}}"
有没有办法用 XAML 中的通用转换器做到这一点?