反向列表框,不进行排序 [英] Reversed Listbox without sorting

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

问题描述

我花了两个星期的时间来尝试找出一种以相反的顺序显示列表框项目的方法,而不使用任何sort属性,并且在我的实体中不放置任何表示逻辑,我只希望最后插入的项目显示在列表框的顶部. 我发现的唯一纯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).

如果您确定绑定是绑定到列表,而不是绑定到列表的元素,则可以使用转换器完成所有操作.假设您使用的是字符串的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}}"/>

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆