Question

I have a bit of a weird situation, I've implemented a ObservableConcurrentDictionary (based on the ConcurrentDictionary from .NET), which I used to implement ObservableConcurrentCollection (which just automatically sets the Key property in the Dictionary, and has all the IList, IEnumerable, IQueryable and ICollection methods). This is all working fine when I do something like:

ObservableConcurrentCollection<string> items = new ObservableConcurrentCollection<string>();
dataGrid.ItemsSource = items;
items.Add("TEST");

This is reflected perfectly in the DataGrid.

However, when I do this:

ObservableConcurrentCollection<string> items = new ObservableConcurrentCollection<string>();
items.Add("TEST"); // <-- Notice that this and the line below are swapped.
dataGrid.ItemsSource = items;

It's not working correctly as an item from 'items' suddenly became a KeyValuePair. I can easily fix this by using dataGrid.ItemsSource = items.Values in the last line, but I'd rather have it working just like the previous one (and it's also confusing).

Was it helpful?

Solution 3

Solved it by changing the ObservableConcurrentCollection, I removed the inheritance and just created a field for the ObservableConcurrentDictionary. Works like a charm now.

OTHER TIPS

Since your new ObservableConcurrentDictionary class is based on ConcurrentDictionary, I suspect the issue is either in the implementation of one of the interface methods.

Without seeing that class, it is hard to diagnose, but I suspect that the problem is in one of the retrieval implementations that is returning the entire dictionary entry rather than the value specified by a key.

For example, when ItemsSource is bound to the grid in the second case, it may retrieve the values through IEnumerable whereas when you call Add in the first case, it is triggering an observable event which contains the correct information.

It should be fairly straightforward to figure out where the problem is occurring by placing a breakpoint in suspect retrieval methods or stepping into the code in the second case as ItemsSource is being asssigned. That should show you fairly quickly which method is being executed and show that the dictionary entry rather than the value is being returned.

for me I always use this

<DataGrid ItemsSource="{Binding}" x:Name="datagrid" >

then

ObservableConcurrentCollection<string> items = new ObservableConcurrentCollection<string>();
items.Add("TEST"); 
dataGrid.DataContext = items;

you can try this, its worknig for me even if i swap my lines

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top