简体   繁体   中英

Data from one listbox used in another listbox

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!

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 (eg "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.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM