0

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?

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68

1 Answers1

1

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.

  • That certainly makes more sense. I have no idea how I screwed it up, but it's happening. I'll give this a go when I'm back in the office tomorrow. Thanks, again Ed. – DonBoitnott Nov 29 '17 at 15:33
  • It worked, of course. I still can't explain why using the `Filter` event was causing dupes, but I suppose it's academic anyway...just wasn't the right approach, regardless. Thanks again. – DonBoitnott Nov 30 '17 at 13:21
  • @DonBoitnott Just some silly thing in there somewhere I bet. Glad you got it going. – 15ee8f99-57ff-4f92-890c-b56153 Nov 30 '17 at 13:22