Reputation: 115
I have two listboxes, one listbox (lbxTeams) contains football teams and certain information about the team (Name and no. of players), My second listbox (lbxPlayers) contains player data (Player Name, Position and Age).
What I want:
On clicking one of the teams from lbxTeams I want all the players and their information to appear in lbxTeams.
At the moment I have a separate class for each listbox ("Team" and "Player").
I don't necessarily need the code of how to do it, I am studying in college so I would actually prefer an explanation or pseudo-code over somebody doing it for me.
Thanks in advance!
Upvotes: 0
Views: 53
Reputation: 562
do you mean like this?
Draft :
----
(lbxTeams)
- TeamName
- NoOfPlayers
(lbxPlayers)
- PlayerName
- Position
- Age
----
Sample Data :
----
(lbxTeams)
----------------------
id (pk) | name
----------------------
t1 | team1
t2 | team2
t3 | team3
(lbxPlayers)
-------------------------------------------------------
name | position | age | noofplayer | teamid (fk)
-------------------------------------------------------
john | left | 22 | 02 | t2
swan | right | 25 | 09 | t2
charlie | mid | 28 | 05 | t3
hane | top | 27 | 07 | t1
----
Design :
----------
|lbxTeams|
----------
| team1 |
| team2 |--> selected - |
| team3 | |
---------- v
--------------
| lbxPlayers |
--------------
---------------------------------------------------------
| name | position | age | noofplayer | teamid (fk)
---------------------------------------------------------
| john | left | 22 | 02 | t2
| swan | right | 25 | 09 | t2
---------------------------------------------------------
----
Illustration :
1. you must create 2 table in the databases.
- table 1
- name:lbxTeams
- fields:
- id (pk)
- name
- table 2
- name:lbxPlayers
- fields:
- name
- position
- age
- noofplayer
- teamid (fk)
2. insert with the sample data.
3. and then in the query databases, you have to filter the
data in the table lbxPlayers is based on table lbxTeams is selected id
4. in the design, if lbxTeams selected 'team1' with id 't2' then just show up the data lbxPlayers with 'teamid' = 't2'
Upvotes: 0
Reputation: 16128
How are you determining which players to display? Do your players all contain a reference to their team? If so then teamListBox.ItemsSource should be bound to the team list and teamListBox.SelectedValue should be bound to property in your model (e.g. "CurrentTeam") which generates the new list of players whenever the user clicks on a team:
private Team _CurrentTeam;
public Team CurrentTeam
{
get { return this._CurrentTeam;}
set {
this.CurrentTeam = value;
OnPropertyChanged("CurrentTeam");
this.CurrentPlayers = this.Players.Where(player => player.Team == value);
}
}
private IEnumerable<Player> _CurrentPlayers;
public IEnumerable<Player> CurrentPlayers
{
get { return this.CurrentPlayers; }
set
{
this.CurrentPlayers = value;
OnPropertyChanged("CurrentPlayers");
}
}
You then bind playerListBox.ItemSource to CurrentPlayers.
Upvotes: 1