How to get a dynamic list of distinct property values of a list?
-
28-09-2019 - |
Question
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.
Solution
You will need to unbind and rebind your list for refresh.
OTHER TIPS
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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow