开发者

Change the comparer for a ListBox

开发者 https://www.devze.com 2022-12-13 11:35 出处:网络
I have a list box that contains a list of WorkItems (from TFS).I want to add items to be selected, but it is failing to identify the items as the same.

I have a list box that contains a list of WorkItems (from TFS). I want to add items to be selected, but it is failing to identify the items as the same.

Here is my code

    public void SelectQueryResultItem(WorkItem item)
    {
        lstQueryResults.SelectedItems.Add(item);
    }

This works great when the Wo开发者_如何学编程rkItem passed in is from the same query that is in the SelectedItems list. However if it came from a different query (ie the object reference is not the same) then it does not identify the object correctly.

I assume it is comparing on reference. I want to override that and make it compare on item.Id. Is there a way to do that?

Note that I am using the WPF ListBox, not the WinForms ListBox


Rather than inherit from ListBox, have you tried overriding Equals on the WorkItem class to do a comparison by ID? It's not ideal since obviously two instances with the same ID are not necessarily equal. But I'm not sure how you might coax the Selector base class into using a custom IComparer.

EDIT I dug in a little deeper and found that you can derive from CollectionView (which is a wrapper around a collection to be bound in WPF) and override its Comparer property to return a custom IComparer. So you could create something like a WorkItemCollectionView that overrides Comparer and return something like a WorkItemIDComparer.

Hope this helps.


Here is a non-cool way to solve this issue.

    public void SelectQueryResultItem(WorkItem item)
    {
        // Because the ListBox only compares by reference, 
        //  we need to find the matching WorkItem (if any)
        //  before adding it to the selected list.
        WorkItem matchingWorkItemInList = GetWorkItemInQueryResultByID(item.Id);
        if (matchingWorkItemInList != null)
            lstQueryResults.SelectedItems.Add(matchingWorkItemInList);
    }

    public WorkItem GetWorkItemInQueryResultListByID(int Id)
    {
        foreach (WorkItem workItem in lstQueryResults.Items)
        {
            if (workItem.Id == Id)
            {
                return workItem;
            }
        }
        return null;
    }

Basically I am circumventing the compare system in the ListBox.

It would be nice if WPF allowed me to have access to the comparer so I can compare by value if I want to and not need to do this.

0

精彩评论

暂无评论...
验证码 换一张
取 消