使用 CustomSort 对 ListCollectionView 进行排序
Sorting ListCollectionView with CustomSort
我正在尝试对 ListCollectionView
进行排序,但无法正常工作。我有自己的 CustomSort
会抛出以下错误:
Cannot implicitly convert type 'SceneComparer' to 'IComparer'. An
explicit conversion exists. (Are you missing a cast?)
这是我的比较器的样子:
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 中的排序
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;
}
我该如何解决这个问题?
CustomSort
是 IComparer
(msdn) 的类型。
示例如下:
http://weblogs.asp.net/monikadyrda/wpf-listcollectionview-for-sorting-filtering-and-grouping
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;
}
}
我正在尝试对 ListCollectionView
进行排序,但无法正常工作。我有自己的 CustomSort
会抛出以下错误:
Cannot implicitly convert type 'SceneComparer' to 'IComparer'. An explicit conversion exists. (Are you missing a cast?)
这是我的比较器的样子:
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 中的排序
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;
}
我该如何解决这个问题?
CustomSort
是 IComparer
(msdn) 的类型。
示例如下:
http://weblogs.asp.net/monikadyrda/wpf-listcollectionview-for-sorting-filtering-and-grouping
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;
}
}