WPF & Linq To SQL binding ComboBox to foreign key

Posted by ZeroDelta on Stack Overflow See other posts from Stack Overflow or by ZeroDelta
Published on 2010-05-14T21:11:11Z Indexed on 2010/05/14 21:14 UTC
Read the original article Hit count: 216

Filed under:
|
|

I'm having trouble binding a ComboBox to a foreign key in WPF using Linq To SQL. It works fine when displaying records, but if I change the selection on the ComboBox, that change does not seem to affect the property to which it is bound.

My SQL Server Compact file has three tables: Players (PK is PlayerID), Events (PK is EventID), and Matches (PK is MatchID). Matches has FKs for the the other two, so that a match is associated with a player and an event.

My window for editing a match uses a ComboBox to select the Event, and the ItemsSource is set to the result of a LINQ query to pull all of the Events. And of course the user should be able to select the Event based on EventName, not EventID.

Here's the XAML:

<ComboBox x:Name="cboEvent" DisplayMemberPath="EventName" 
     SelectedValuePath="EventID" 
     SelectedValue="{Binding Path=EventID, 
     UpdateSourceTrigger=PropertyChanged}"  />

And some code-behind from the Loaded event handler:

var evt = from ev in db.Events
                      orderby ev.EventName
                      select ev;

cboEvent.ItemsSource = evt.ToList();

var mtch = from m in db.Matches
           where m.PlayerID == ((Player)playerView.CurrentItem).PlayerID
           select m;

matchView = (CollectionView)CollectionViewSource.GetDefaultView(mtch);

this.DataContext = matchView;

When displaying matches, this works fine--I can navigate from one match to the next and the EventName is shown correctly. However, if I select a new Event via this ComboBox, the CurrentItem of the CollectionView doesn't seem to change. I feel like I'm missing something stupid!

  • Note: the Player is selected via a ListBox, and that selection filters the matches displayed--this seems to be working fine, so I didn't include that code. That is the reason for the "PlayerID" reference in the LINQ query

© Stack Overflow or respective owner

Related posts about wpf

Related posts about linq-to-sql