简体   繁体   中英

Why does my sort behavior fire with only one item in the list, and only the first time the collection is added to?

This question is a result of the fix to this problem . After getting the sort behavior to properly sort the ObservableCollection , I now notice that the first time the collection is added to, the CustomSorter handler fires with only the first item in it, and that same item is passed in as both parameters to the method. That is producing a duplicate item in the list.

Here are the pertinent parts of the view model code:

public ObservableCollection<PermissionItemViewModel> PermissionItems { get; private set; }

private void FetchRoleData()
{
    PermissionItems.Clear();

    if (SelectedRole != null)
    {
        using (var context = new myDataContext(new myDbFactory().GetConnectionString()))
        {
            foreach (PermissionsEnum permission in Enum.GetValues(typeof(PermissionsEnum)))
                PermissionItems.Add(new PermissionItemViewModel(permission, SelectedRole[permission]));
        }
    }
}

All subsequent manipulations of that collection do not do this...it only happens the first time through the FetchRoleData method. Why?

EDIT:

Some additional information. The CustomSort property is set when the CollectionViewSource fires its Filter event (the only event it has AFAIK). I couldn't come up with any better trigger to get it set. The OnAttached override is too soon, as the View member of the CollectionViewSource is not set yet by that point. Catch 22, I guess. That is happening immediately after that first collection item is added. If this is due to the order in which things are being set, then are there any recommendations for a change?

I don't know how or where you're setting up the filter handler. Here's an example of how to set a custom sort on a CollectionViewSource when its View property changes. That's when you want to do it. This assumes that it's in the resources for a Window (or at least someplace where the Window can find it). But once you have cvs , wherever it comes from and however you got your mitts on it, the rest is the same.

public MainWindow()
{
    InitializeComponent();

    var cvs = FindResource("MyCollectionViewSource1") as CollectionViewSource;

    var dpdView = DependencyPropertyDescriptor.FromProperty(
                      CollectionViewSource.ViewProperty, typeof(CollectionViewSource));

    dpdView.AddValueChanged(cvs, (s, e) =>
    {
        if (cvs.View is ListCollectionView lvc)
        {
            lvc.CustomSort = new CustomSorter();
        }
    });
}

I'm baffled by your claim that the first item in the collection is being duplicated. No code you've shown, and no code I've given you, could have that effect. You'll have to share code that demonstrates that issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM