Reputation: 3570
I have a list of objects, say Cars, that I want to bind to a single WinForm.
The purpose of this WinForm is to edit common properties of all bound Cars e.g. lets say, IsRoadWorthy.
How do I bind a checkbox to the .IsRoadWorthy property of all Cars in the list such that, if all cars are road worthy, then the checkbox.Checked property is true, if all cars not road worthy, then the checkbox.Checked property is false and anything in between, then checkbox.Checked property is 'indeterminate'.
NOTE: I'm aware I can restrict the user from setting the Indeterminate state by setting the Checkbox.ThreeState property to false and then setting the Checkbox.State to Indeterminate in code.
Upvotes: 2
Views: 148
Reputation: 82474
You can use Linq's extension method All
for this:
var cars = new List<Car>();
checkbox.CheckedState = (cars.All(c => c.IsRoadWorthy)) ? CheckState.Checked :
(cars.All(c => !c.IsRoadWorthy)) ? CheckState.Unchecked
CheckState.Indeterminate;
Upvotes: 1