简体   繁体   English

为什么我的排序行为仅在列表中只有一个项目时发生,并且只有在第一次添加集合时才发生?

[英]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. 在获得正确对ObservableCollection进行排序的排序行为之后,我现在注意到,第一次添加该集合时, CustomSorter处理程序将仅使用其中的第一项触发,并且该同一项作为两个参数传递给该方法。 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. 该集合的所有后续操作都不会执行此操作……它仅通过FetchRoleData方法第一次发生。 Why? 为什么?

EDIT: 编辑:

Some additional information. 一些其他信息。 The CustomSort property is set when the CollectionViewSource fires its Filter event (the only event it has AFAIK). CollectionViewSource触发其Filter事件(唯一具有AFAIK的事件)时,将设置CustomSort属性。 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. 由于尚未设置CollectionViewSourceView成员,因此OnAttached重写太早了。 Catch 22, I guess. 赶上22,我想。 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. 这是一个当View属性更改时如何在CollectionViewSource上设置自定义排序的示例。 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. 但是,一旦有了cvs ,无论它来自何处,但是无论您有什么手套,其余的都是一样的。

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. 您必须共享演示该问题的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 DropDownList 的 SelectedItem 每次只显示列表中的第一项? - Why is my DropDownList's SelectedItem only showing the first item in the list each time? 为什么仅将我的一个控件添加到此表单? - Why does only one of my controls get added to this form? 为什么我的itemCommand DataGrid事件仅在网格控件中第二次单击项目时触发? - Why does my itemCommand DataGrid event only fire on the second click of an item in the grid control? 为什么我的列表框仅显示一项? - Why is my listbox only displaying one item? 为什么ArrayList.Sort()仅按第一位排序? - Why does ArrayList.Sort() sorting by only first digit? 如何使计时器只发射一次 - How to make a timer fire only one time HTM BeginCollectionItem 仅返回集合的第一项 - HTM BeginCollectionItem returns only first item of collection 为什么我的角色每次不动时只向目标旋转一次? - Why does my character only rotate to the target one time every time it doesn't move? 为什么我的MVC Razor仅在第一次返回视图时才填充HTML表? - Why does my MVC Razor populate the HTML table only the first time the view is returned? 为什么只将第一个RadioButton添加到GroupBox? - Why is only the first RadioButton being added to the GroupBox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM