[英]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. 由于尚未设置CollectionViewSource
的View
成员,因此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.