I have a TextBlock
in my UI bound to the .Count()
extension of a property. I would like to add LINQ filtering to the property, but when I do it breaks the binding.
This works and updates the UI without any problems:
public IEnumerable<Worker> WorkersTest
{
get
{
return DataManager.Data.Workers;
}
}
After adding LINQ filtering, WorkersTest
does not update the TextBlock
, though a MessageBox confirms that the property is being modified. Upon initialization of the data, however, the value displays correctly:
public IEnumerable<Worker> WorkersTest
{
get
{
return DataManager.Data.Workers.Where(w => w.Gender == Gender.Male);
}
}
Is there any way to keep the LINQ filtering without breaking the binding?
Update: One thing that I need to clarify is that DataManager.Data.Workers
is an ObservableCollection
.
From what I can tell, any LINQ operation breaks the binding with the exception of AsEnumerable()
, which doesn't have any effect.
Also, in response to Andres' answer, Count()
isn't the problem. If I bind this property to a DataGrid
, I get the same results. With LINQ, it doesn't update. Without LINQ, it does. Is there any sort of workaround for this, preferably one that doesn't involve ICollectionView
or anything of that nature?
Update: Does anybody know if LINQ extensions such as Where
return new objects when used? I haven't been able to find any documentation of this online. If this is the case, however, that may be the problem.
Update: I inserted a breakpoint and found some interesting information that may be useful:
Without LINQ:
With LINQ:
Perhaps this can shed some light on what's really going on and hopefully get me closer to the solution.
Would I be correct in assuming that CollectionChanged
and PropertyChanged
being null is the source of the problem? If so, what can I do to fix that?