別のバインディングを作成する必要がないため、ObservableCollection をアルファベット順に並べたいと思います。Linq と OrderBy メソッドを使用できることを見てきました。複雑なデータ (リストを他のリストにまとめる) を整理する必要があるため、個人用の IComparer も使用したいと思いますが、VS は独自の比較器を使用できないと言っています。
(私のVSはフランス語なので、エラーのリンクを教えてください)
http://msdn.microsoft.com/en-gb/library/hxfhx4sy%28v=vs.90%29.aspx
何か案は ?
private void SortGraphicObjectsAscending(object sender, RoutedEventArgs e)
{
GraphicObjects.OrderBy(graphicObject => graphicObject.Nom, new GraphicObjectComparer(true));
}
そして、私自身の比較者(すでにテストされています)
public class GraphicObjectComparer : IComparer<GraphicObject>
{
int order = 1;
public GraphicObjectComparer(Boolean ascending)
{
if (!ascending)
{
order = -1;
}
}
public GraphicObjectComparer()
{
}
public int Compare(GraphicObject x, GraphicObject y)
{
return String.Compare(x.Nom, y.Nom, false) * order;
}
}
回答ありがとうございます。とにかく、私には別の問題があります。マイケルが言ったように、私はより複雑な比較を使用しますが、別のエンティティを対象としています。このエンティティは階層ツリーで表示され、オブジェクトには同じタイプの他のオブジェクトのリストが含まれる場合があります。
DBにアクセスできないため、GraphicObjectをテストできませんでした(現時点ではオブジェクトがありませんでした)。VideoEntity のテストでは、ObservableCollection が希望どおりに並べ替えられていないようです (別のものを作成します)。アルファベット順に逆にしたいのですが、うまくいきません。
public class VideoEntityComparer : IComparer<VideoEntity>
{
int order = 1;
public VideoEntityComparer(Boolean ascending)
{
if (!ascending)
{
this.order = -1; // so descending
}
}
public VideoEntityComparer()
{
}
public int Compare(VideoEntity x, VideoEntity y)
{
if ((x is BaseDirectory && y is BaseDirectory) || (x is BaseSite && y is BaseSite) || (x is VideoEncoder && y is VideoEncoder))
{
return string.Compare(x.Nom, y.Nom, false) * order; // only objects of the same type are sorted alphabetically
}
else if ((x is BaseDirectory && y is BaseSite) || (x is BaseSite && y is VideoEncoder))
{
return -1;
}else
{
return 1;
}
}
}
private void SortDirectoriesDescending(object sender, RoutedEventArgs e)
{
ObservableCollection<BaseDirectory> tempDir = new ObservableCollection<BaseDirectory>(
Directories.OrderBy(directory => directory, new VideoEntityComparer(false)));
Directories = tempDir;
}
PS : ところで、私は DependancyProperty に基づいて行動しています。それは正しい方法ですか?(私はWPFが初めてです)