Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用用于SortDescription的自定义比较器对字典(按键)进行排序_C#_Xaml_Sorting_Compare_Collectionviewsource - Fatal编程技术网

C# 使用用于SortDescription的自定义比较器对字典(按键)进行排序

C# 使用用于SortDescription的自定义比较器对字典(按键)进行排序,c#,xaml,sorting,compare,collectionviewsource,C#,Xaml,Sorting,Compare,Collectionviewsource,我有一个属性:observedictionary ModuleConnections,并创建了一个集合视图源,如下所示: <CollectionViewSource x:Key="ModuleConnectionSorter" Source="{Binding ModuleConnections}"> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyNa

我有一个属性:
observedictionary ModuleConnections
,并创建了一个集合视图源,如下所示:

<CollectionViewSource x:Key="ModuleConnectionSorter" Source="{Binding ModuleConnections}">
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="Key" Direction="Ascending"/>
     </CollectionViewSource.SortDescriptions>
</CollectionViewSource>
我还创建了自己的比较器:

public class AddressComparer : IComparer<string>
{
    public int Compare(string firstAddress, string secondAddress)
    {
        uint firstTddressInteger = AddressToUint(firstAddress);
        uint secondAddressInteger = AddressToUint(secondAddress);
        if (firstAddressInteger < secondAddressInteger)
        {
            return -1;
        }
        else if (firstAddressInteger > secondAddressInteger)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    private static uint AddressToUint(string Address)
    {
        var addressIP = IPAddress.Parse(Address).GetAddressBytes();
        Array.Reverse(addressIP);
        return BitConverter.ToUInt32(addressIP, 0);
    }
}
但是,我无法执行此操作,因为我得到一个带有以下错误的“System.InvalidCastException”:

Cannot convert object of type MS.Internal.Data.EnumerableCollectionView to System.Windows.Data.ListCollectionView.
这可能与以下事实有关:我没有绑定列表,而是绑定了(实际上与字典相同)

因此,由于我不能以正常的方式(无论如何我都不喜欢这种方式),我想如果你愿意的话,我会创建我自己的SortDescription一个CustomSortDescription。当我想到这样做的时候,我想知道为什么没有其他人这样做,很快我就明白了,这也不是一个解决办法

我认为(天真的我)我会简单地继承sortdescription并创建自己的派生版本:

public class CustomSortDescription : SortDescription
有人能帮我把事情弄清楚吗?我只想对键(字符串)上的ObservableDictionary进行排序

更新:
我做了一个工作,没有太多的工作,更多的是另一种工作方式。但我现在做的是,每次我在字典中添加内容时,我都会对字典进行如下排序:

var sortedIOrderedEnumerable = this.ModuleConnections.OrderBy(keyValuePair => keyValuePair.Key, new TCAddressComparer());
mModuleConnections = new ObservableDictionary<string, ModuleConnection>(sortedIOrderedEnumerable.ToDictionary(keyValuePair => keyValuePair.Key, keyValuePair => keyValuePair.Value));
RaisePropertyChanged(() => this.ModuleConnections);
var sortedIOrderedEnumerable=this.ModuleConnections.OrderBy(keyValuePair=>keyValuePair.Key,new TCAddressComparer());
mModuleConnections=newobservedictionary(sortedIOrderedEnumerable.ToDictionary(keyValuePair=>keyValuePair.Key,keyValuePair=>keyValuePair.Value));
RaisePropertyChanged(()=>this.ModuleConnections);

这不是应该怎么做的,我仍然在寻找一种在XAML代码中对视图(而不是数据源)进行排序的方法。

您能试试view.ToList()吗。view.ToList().CustomSort=(IComparer)new TCAddressComparer()@不幸的是,AnılOkay CollectionViewSource没有ToList方法。我想知道Vincent,你是否找到了一种方法来实现这一点,因为我遇到了类似的问题。不,我坚持更新中发布的“解决方法”,就是这样。不幸的是,我从来没有时间重温这一点
public class CustomSortDescription : SortDescription
var sortedIOrderedEnumerable = this.ModuleConnections.OrderBy(keyValuePair => keyValuePair.Key, new TCAddressComparer());
mModuleConnections = new ObservableDictionary<string, ModuleConnection>(sortedIOrderedEnumerable.ToDictionary(keyValuePair => keyValuePair.Key, keyValuePair => keyValuePair.Value));
RaisePropertyChanged(() => this.ModuleConnections);