Thibault Falise
Thibault Falise

Reputation: 5885

How to get a dynamic list of distinct property values of a list?

I am trying to bind a ComboBox control to a list of strings extracted from a list of custom objects.

Here is the object I'm using :

public class Operation
{
    public DateTime ValueDate { get; set; }
    public int Amount { get; set; }
    public string Category { get; set; }
}

What I'm trying to do is binding the combo box used to input a new Operation's Category to the list of distinct categories already existing in a list of Operations.

Example :

List of Operations :

{04/12/2010, 100, "Home"}
{05/12/2010, 100, "Home"}
{05/12/2010, 200, "Entertainment"}

Available in the auto-complete list of the combobox : "Home", "Entertainment".

Currently, I am able to get a static list of the available categories existing in the list, but I am unable to get the list updated when I add a new Operation to the existing list.

Upvotes: 1

Views: 527

Answers (2)

alpha-mouse
alpha-mouse

Reputation: 5003

You can do this:

_combo.ItemsSource = _operationsCollection;
_combo.DisplayMemberPath = "Category";
_combo.Items.Filter = Filter;

private bool Filter(object operationObj)
{
    var operation = (Operation)operationObj;
    var first = _operationsCollection.First(p => p.Category == operation.Category);
    return ReferenceEquals(operation, first);
}

Upvotes: 0

Will Marcouiller
Will Marcouiller

Reputation: 24132

You will need to unbind and rebind your list for refresh.

Upvotes: 1

Related Questions