没有排序的反向列表框

没有排序的反向列表框

本文介绍了没有排序的反向列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了两周时间试图找出一种方法来以相反的顺序显示列表框的项目,而不使用任何排序属性,也没有在我的实体中放置任何表示逻辑,我只希望最后插入的项目显示在列表框的顶部.我发现的唯一纯 XAML 解决方案是 WPF 反转 ListView 但它并不那么优雅.我还尝试覆盖我的 BindableCollection 的 GetEnumerator() (我使用 Caliburn Micro 作为 MVVM 框架)以返回一个枚举器,该枚举器以相反的顺序迭代我的集合的项目,但 id 不起作用.我该怎么办?

I spent last two weeks trying to figure out a method to display the items of a listbox in reversed order without using any sort property and without putting any presentation logic in my entities, I just want that the last inserted item is displayed at the top of the listbox.The only pure XAML solution I've found is this WPF reverse ListView but it isn't so elegant. I've also tried to override the GetEnumerator() of my BindableCollection (I use Caliburn Micro as MVVM framework) to return an enumerator that iterate over my collection's items in the reverse order but id did not work.How can I do?

推荐答案

ScaleTransform 是针对这种特殊情况的优雅解决方案,但可能有更多通用应用程序(例如绑定相同的列表但应用了不同的排列).

ScaleTransform is an elegant solution to this particular case, but there could be more generic applications (such as binding the same list but with different permutations applied).

p>

如果您确保考虑绑定到列表而不是列表的元素,则可以使用转换器完成所有这些操作.假设您使用字符串的 ObservableCollection(确保可以使用泛型和反射来使其更优雅)并且错过了所有正确的异常处理编码和对 Convert 的多次调用...

It is possible to do this all with converters if you make sure to consider that the binding is to the list, rather than the elements of the list. Assuming that your using an ObservableCollection of strings (sure it would be possible to use generics and reflection to make this more elegant) and missing out all the proper coding of exception handling and multiple calls to Convert...

public class ReverseListConverter : MarkupExtension, IValueConverter
{
    private ObservableCollection<string> _reversedList;

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        _reversedList = new ObservableCollection<string>();

        var data = (ObservableCollection<string>) 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<string>)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();
    }
}

然后您可以在 XAML 中使用类似的东西进行绑定

You can then bind within your XAML with something like

<ListBox ItemsSource="{Binding Converter={ReverseListConverter}}"/>

这篇关于没有排序的反向列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:16