I’m attempting to create a small application in which I can make a poul for the coming Europian Soccer Championship.
For this, I need to add matches ofcourse.
What I wish for is to have 3 comboboxes where the first is set by what type of match it is (Poule A, Poule B etc). After that combobox has been set, I want the next two comboboxes only to show the teams that are in those poules.
I believe this can be done using converters but I can’t seem to get it to work.. or is there a better approach ?
current code:
<ComboBox ItemsSource="{Binding MatchTypes}"
DisplayMemberPath="TypeName"
Grid.Row="1" />
<ComboBox ItemsSource="{Binding Teams}"
DisplayMemberPath="TeamName"
Grid.Column="1"
Grid.Row="1" />
<ComboBox ItemsSource="{Binding Teams}"
DisplayMemberPath="TeamName"
Grid.Column="2"
Grid.Row="1" />
Is there a simple way (linq?) to query the last two comboboxes for only the teams that are in the poule selected in the first combobox?
If possible, I prefer to keep this out of the viewmodel and use a converter or something similar.
Personally, I would keep this in the viewmodel code. I have done something similar, so here it is, mapped to what you are doing:
I have a pre-populated list of items in my viewmodel. This would be MatchTypes.
I would have another property called CurrentMatchType, using INotifyPropertyChanged when it is set.
When the value of CurrentMatchType is set, it would call out to the datasource and populate the other two lists on the viewmodel. We also have 2 variable called PouleA and PouleB, representing the final team selections. We will call the lists you just grabbed from the server TeamsA and TeamsB. It’s the same data for both lists, but I would set the data source result to an internal value and then set TeamsA to the list of all teams except the one selected in PouleB, and the list of TeamsB to the list of all the teams except the ones in PouleA. This way, one team cannot be matched by themselves.
One last thing I forgot: On the setter of PouleA and PouleB, you would run the same code as above to filter the available teams so the opposite team is also excluded. Since INPC is hooked up to everything, your comboboxes will all change automatically.
When I grab data from a datasource, I expose a property to have a BusyIndicator take over the screen so nothing can be touched until it’s done grabbing data.
I’m in the camp that thinks trying to use a converter for things like this adds unnecessary frustration. If you don’t want to add it to your viewmodel because you’re reusing it in different places, there’s nothing stopping you from making a new viewmodel that exposes the old viewmodel as a property.
PSEUDO-CODE