Implementing a ContextMenu column in DataGrid

Sep 17, 2008 at 11:53 PM
I preface this by stating I am a rookie WPF developer. I've been reading up all I can the last few days and still haven't been able to get past my problem. Any help would be appreciated:

I am using the DataGrid to Display Tennis Matches

The items in my DataGrid are of type TennisMatch. Each TennisMatch has a CourtNumber (Type: int), HomePlayer (Type: Player), AwayPlayer (Type: Player) and Score (Type: string) each displayed in a column of the DataGrid.

I also have a static Data class holds the source of my DataGrid --> MatchList (Type: ObservableCollection<TennisMatch>) as well as a RosterList (Type: ObservableCollection<Roster>) containing a list of Roster objects that each contain a PlayerList (Type: List<Player>).

I have a TabControlRosters on my GUI that shows all of the rosters. For each Roster in RosterList I add a TabItem to the TabControl and set TabItem.DataContext to Roster.PlayerList. The TabItem's ControlTemplate causes the TabItem to display a ListView containing all of my players and info about them.

Populating my DataGrid works great: 
<dg:DataGrid ItemsSource="{Binding Source={x:Static DakStatsTennis:Data.matchList}}" AutoGenerateColumns="False" Name="listViewMatchList" AlternationCount="2" >
    <dg:DataGrid.Columns>
        <dg:DataGridTextColumn DataFieldBinding="{Binding Path=CourtNumber}" Header="Crt" />
        <dg:DataGridComboBoxColumn DataFieldBinding="{Binding Path=HomePlayer, Converter={StaticResource PlayerConverter}, ConverterParameter=Name }" Header="Home Player" />
        <dg:DataGridComboBoxColumn DataFieldBinding="{Binding Path=AwayPlayer, Converter={StaticResource PlayerConverter}, ConverterParameter=Name }"  Header="Away Player" />
        <dg:DataGridCheckBoxColumn DataFieldBinding="{Binding Path=IsActiveOutput}" Header="Show" />
        <dg:DataGridTextColumn Header="Del" />
    </dg:DataGrid.Columns>
</dg:DataGrid>


But now I want to be able to edit the players of the matches. I've got two choices, and I can't figure out either one.

Option A)
Select a Player cell, and to edit it, I can pick a player from any of the ListViews in my TabControlRosters. (First click the Tab representing the player's team, then click the player from the ListView on that tab and press Enter or some other verification action)

Option B)
Add a HomeTeam and AwayTeam columns in my DataGrid. Then to choose a different HomePlayer, I'll first click the HomeTeam Cell, opening a comboBox listing all of the teams in Data.RosterList, then click the HomePlayer Cell opening a comboBox listing all of the Players in Data.RosterList[index of selected team].PlayerList

Rather than pre-loading Data.MatchList, the eventual goal is to create all of the TennisMatches through the DataGrid and be able to choose the HomePlayer and AwayPlayer from pre-loaded Rosters in some form or another.

Any thoughts, or is my design completely flawed to begin with???

Thanks for the help and I apologize if I did a terrible job of explaining the situation,
Scott
Sep 18, 2008 at 10:25 PM
Well... I'm going to give up on option A... and try Option B but with a twist.

When I edit a cell in a Player column, I would like to implement a Context Menu instead of a ComboBox. So editing a Player cell brings up a drop down list containing a list of teams (bound from Data.RosterList) and each team will have an arrow on the right side that brings up another list of Players for that team (bound from Data.RosterList[SelectedRoster].PlayerList)

Is the only way to implement a DataGridContextMenuColumn by using DataGridTemplateColumn? And if so... has anybody else tried this and have a sample?

Thanks,
Scott