首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用ListCollectionView对CustomSort进行排序

用ListCollectionView对CustomSort进行排序
EN

Stack Overflow用户
提问于 2015-08-18 18:53:50
回答 1查看 5.2K关注 0票数 0

我正在尝试对一个ListCollectionView进行排序,但我无法让它工作。我有自己的CustomSort,它引发以下错误:

无法隐式地将类型“SceneComparer”转换为“IComparer”。存在显式转换。(你错过了演员阵容吗?)

这就是我的比较者的样子:

代码语言:javascript
复制
public class SceneComparer : IComparer<Scene>
{
    // Assumes that a scene is either numeric, or numeric + alpha.
    private readonly Regex sceneRegEx = new Regex(@"(\d*)(\w*)", RegexOptions.Compiled);

    public int Compare(Scene x, Scene y)
    {
        var firstSceneMatch = this.sceneRegEx.Match(x.SceneNumber);

        var firstSceneNumeric = Convert.ToInt32(firstSceneMatch.Groups[1].Value);
        var firstSceneAlpha = firstSceneMatch.Groups[2].Value;

        var secondSceneMatch = this.sceneRegEx.Match(y.SceneNumber);

        var secondSceneNumeric = Convert.ToInt32(secondSceneMatch.Groups[1].Value);
        var secondSceneAlpha = secondSceneMatch.Groups[2].Value;

        if (firstSceneNumeric < secondSceneNumeric)
        {
            return -1;
        }

        if (firstSceneNumeric > secondSceneNumeric)
        {
            return 1;
        }

        return string.CompareOrdinal(firstSceneAlpha, secondSceneAlpha);
    }
}

下面是ViewModel中的排序

代码语言:javascript
复制
private ListCollectionView _scenesNavigator;
public ListCollectionView ScenesNavigator
{
    get
    {
        _scenesNavigator = SceneCollectionView;
        _scenesNavigator.CustomSort = new SceneComparer(); ----> ERROR
        _scenesNavigator.IsLiveSorting = true;

        return _scenesNavigator;
    }
    set
    {
        _scenesNavigator = value; RaisePropertyChanged();
    }
}

public SourceViewModel()
{
        SceneCollectionView = Application.Current.Resources["SceneCollectionView"] as ListCollectionView;
}

我怎样才能解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-18 19:05:02

CustomSortIComparer (msdn )的类型。

下面是一个例子:

http://weblogs.asp.net/monikadyrda/wpf-listcollectionview-for-sorting-filtering-and-grouping

代码语言:javascript
复制
public class SortCreaturesByAge : IComparer
{
    public int Compare( object x, object y )
    {
        if( x as CreatureModel == null && y as CreatureModel == null )
        {
            throw new ArgumentException( "SortCreatures can 
                                    only sort CreatureModel objects." );
        }
        if( ((CreatureModel) x).Age > ((CreatureModel) y).Age )
        {
            return 1;
        }
        return -1;
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32080494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档