Click here to Skip to main content
15,886,963 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: WPF - Why? Pin
RedDk17-Oct-14 11:29
RedDk17-Oct-14 11:29 
GeneralRe: WPF - Why? Pin
SteveHolle6-Nov-14 4:18
SteveHolle6-Nov-14 4:18 
GeneralRe: WPF - Why? Pin
Mycroft Holmes17-Oct-14 13:51
professionalMycroft Holmes17-Oct-14 13:51 
GeneralRe: WPF - Why? Pin
SteveHolle6-Nov-14 4:22
SteveHolle6-Nov-14 4:22 
GeneralRe: WPF - Why? Pin
Mycroft Holmes6-Nov-14 11:36
professionalMycroft Holmes6-Nov-14 11:36 
GeneralRe: WPF - Why? Pin
Gary R. Wheeler8-Nov-14 3:12
Gary R. Wheeler8-Nov-14 3:12 
GeneralRe: WPF - Why? Pin
Pete O'Hanlon11-Nov-14 20:46
mvePete O'Hanlon11-Nov-14 20:46 
QuestionProblem to synchronise two DataContext bound ComboBoxes Pin
Tom Rahav10-Oct-14 18:25
Tom Rahav10-Oct-14 18:25 
Hi,
I’m trying to do something quite simple, I believe, but am very new to WPF and can’t figure it out. I want to display bank account details of a specific employee (which I select separately), and I want to let the user change those details if needed. My problem concerns specifically the following 3 tables:

Banks:
- BankID (PK)
- CountryID
- BankName
- ...

Countries:
- CountryID (PK)
- CountryName
- ...

BankAccounts:
- BankAccountID (PK)
- BankID
- ...

On my application’s MainWindow I have 2 ComboBoxes. I want to load the first one (cmbBankCountry) with a list of all the countries from ‘Countries’ table, and then automatically select the country which corresponds to the employee’s bank account.

Similarly, I want the second ComboBox (cmbBanks) to be loaded with the names of all the banks that are associated with the selected country, and here too, select automatically the bank associated with that employee’s bank account.

Of course, if the user changes the selection of the country for that bank account, the second ComboBox should be reloaded and display only the banks associated with the newly selected country.

All the data comes from an SQL database via Linq-To-SQL classes (dbml), which I generated to represent the database tables. I.e. I have a Bank class, a Country class, a BankAccount class, etc. defined in a DataContext that I declared as a Private Shared variable of Application. I also created a ReadOnly property called DBMain to access it.

So far I tried to tackle this task as follows:
To load the first Combobox with all the countries I placed the following code in the MainWindow_Loaded event:
VB
Dim lstCountries As List(Of Country) = Application.DBMain.GetCountries()
cmbBankCountry.ItemsSource = lstCountries
cmbBankCountry.DisplayMemberPath = "CountryName"
cmbBankCountry.SelectedValuePath = "CountryID"

The GetCountries function looks like this:
VB
Public Function GetCountries() As List(Of Country)
 
            Dim lstCountry As List(Of Country) = (From c In Application.DBMain.Countries
                                                                      Select c
                                                                    ).ToList
            Return lstCountry
 
End Function

To load the second Combobox with the corresponding banks, I created an event that is triggered when the first Combobox’s selection (country) is changed:
VB
Private Sub DisplayBanksForCountry()
 
        Dim lstBanks As List(Of Bank) = Application.DBMain.GetBanks(cmbBankCountry.SelectedValue)
        cmbBanks.ItemsSource = lstBanks
        cmbBanks.DisplayMemberPath = "BankName"
        cmbBanks.SelectedValuePath = "BankID"
 
End Sub

And the GetBanks function looks like this:
VB
Public Function GetBanks(inCountryID As Integer) As List(Of Bank)
 
            Dim lstBanks As List(Of Bank) = (From b In Application.DBMain.Banks
                                             Where b.CountryID = inCountryID
                                                           Select b).ToList
 
            Return lstBanks
 
End Function

Finally, the definition of the two ComboBoxes in the XAML file looks like this:
VB
<ComboBox x:Name="cmbBankCountry" MinWidth="120" Grid.Row="0" Grid.Column="1" SelectedValue="{Binding Path=b.BankCountryID}" SelectionChanged="DisplayBanksForCountry"/>
 
<ComboBox x:Name="cmbBanks" MinWidth="120" Grid.Row="1" Grid.Column="1" SelectedValue="{Binding Path=b.BankID}"/>

In this “solution” the first ComboBox is working just fine. It is loaded with all the countries and shows automatically the country of the employee’s bank account.

However, when the Window is first loaded, the second ComboBox is empty. It is loaded with banks only after I change the selection of the first ComboBox. Obviously, this is what I asked it to do, but I was assuming (and hoping…) that the SelectionChanged event of the first ComboBox (cmbBankCountry) will occur when the ComboBox is set to display the account’s country by Binding. Unfortunately, that is not the case.

I guess that there is a very elegant way to do it via Binding between the two ComboBox, but my brain is still wired to the old Windows Forms, events oriented way of doing things.

Thank a lot in advance for any help!
AnswerRe: Problem to synchronise two DataContext bound ComboBoxes Pin
Gerry Schmitz13-Oct-14 11:39
mveGerry Schmitz13-Oct-14 11:39 
GeneralRe: Problem to synchronise two DataContext bound ComboBoxes Pin
Tom Rahav13-Oct-14 14:39
Tom Rahav13-Oct-14 14:39 
QuestionDataGrid won't detect CTRL + C Pin
Mc_Topaz9-Oct-14 3:17
Mc_Topaz9-Oct-14 3:17 
AnswerRe: DataGrid won't detect CTRL + C Pin
Vincent Beek9-Oct-14 15:23
Vincent Beek9-Oct-14 15:23 
QuestionRe: DataGrid won't detect CTRL + C Pin
Mc_Topaz9-Oct-14 20:08
Mc_Topaz9-Oct-14 20:08 
AnswerRe: DataGrid won't detect CTRL + C Pin
Vincent Beek9-Oct-14 20:25
Vincent Beek9-Oct-14 20:25 
AnswerRe: DataGrid won't detect CTRL + C Pin
Hi I am Kevin29-Oct-14 6:05
Hi I am Kevin29-Oct-14 6:05 
QuestionViewBox Button alignment Pin
Member 102727486-Oct-14 10:00
Member 102727486-Oct-14 10:00 
AnswerRe: ViewBox Button alignment Pin
Mycroft Holmes6-Oct-14 14:21
professionalMycroft Holmes6-Oct-14 14:21 
QuestionDisplaying Validation Pin
cjb1102-Oct-14 21:49
cjb1102-Oct-14 21:49 
AnswerRe: Displaying Validation Pin
cjb1102-Oct-14 23:05
cjb1102-Oct-14 23:05 
QuestionReport Not Showing All Data Pin
Kevin Marois29-Sep-14 10:20
professionalKevin Marois29-Sep-14 10:20 
QuestionWPF Reporting Question Pin
Kevin Marois23-Sep-14 7:07
professionalKevin Marois23-Sep-14 7:07 
AnswerRe: WPF Reporting Question Pin
Bernhard Hiller23-Sep-14 21:56
Bernhard Hiller23-Sep-14 21:56 
GeneralRe: WPF Reporting Question Pin
Kevin Marois24-Sep-14 16:00
professionalKevin Marois24-Sep-14 16:00 
QuestionForm with ComboBoxes bound to the same source. Pin
cjb11016-Sep-14 2:45
cjb11016-Sep-14 2:45 
AnswerRe: Form with ComboBoxes bound to the same source. Pin
Pete O'Hanlon16-Sep-14 3:16
mvePete O'Hanlon16-Sep-14 3:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.