来自另一个列表框中使用的一个列表框中的数据

时间:2013-12-01 22:37:22

标签: c# wpf listbox

我有两个列表框,一个列表框(lbxTeams)包含足球队和关于球队的某些信息(名称和球员号码),我的第二个列表框(lbxPlayers)包含球员数据(球员姓名,位置和年龄)。 / p>

我想要的是什么:

点击lbxTeams的其中一支球队后,我希望所有球员及其信息都出现在lbxTeams中。

目前,每个列表框都有一个单独的类(“团队”和“播放器”)。

我不一定需要如何操作的代码,我在大学学习,所以我实际上更喜欢解释或伪代码而不是为我做这件事的人。

提前致谢!

2 个答案:

答案 0 :(得分:1)

您如何确定要播放哪些播放器?你的球员都包含他们球队的参考吗?如果是这样,那么teamListBox.ItemsSource应该绑定到团队列表,teamListBox.SelectedValue应该绑定到模型中的属性(例如“CurrentTeam”),当用户点击团队时,它会生成新的播放器列表:

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");
    }
}

然后将playerListBox.ItemSource绑定到CurrentPlayers。

答案 1 :(得分:0)

你是说这个意思吗?

草稿:

----
(lbxTeams)
- TeamName
- NoOfPlayers

(lbxPlayers)
- PlayerName 
- Position
- Age
----

示例数据:

----
(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
----

设计:

 ----------
 |lbxTeams|
 ----------
 | team1  | 
 | team2  |--> selected - |
 | team3  |               |
 ----------               v
                    --------------
                    | lbxPlayers |
                    --------------
                    ---------------------------------------------------------
                    | name       | position | age | noofplayer | teamid (fk)
                    ---------------------------------------------------------
                    | john       |  left    | 22  | 02         | t2
                    | swan       |  right   | 25  | 09         | t2
                    ---------------------------------------------------------
----

插图:

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'