绑定两个ComboBox的问题 [英] Problem in binding two ComboBoxes

查看:78
本文介绍了绑定两个ComboBox的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我相信,我正在尝试做一些非常简单的事情,但我对WPF很新,并且无法理解。我想显示特定员工的银行帐户详细信息(我单独选择),我想让用户在需要时更改这些详细信息。我的问题具体涉及以下3个表:



银行:

- BankID(PK)

- CountryID

- 银行名称

- ...



国家/地区:

- CountryID(PK)

- CountryName

- ...



BankAccounts:

- BankAccountID(PK)

- BankID

- ...



在我的应用程序的MainWindow上,我有2个ComboBoxes。我想加载第一个(cmbBankCountry),其中列出了来自国家/地区表格的所有国家/地区,然后自动选择与该员工的银行帐户对应的国家/地区。



同样,我希望第二个ComboBox(cmbBanks)加载与所选国家/地区相关联的所有银行的名称,此处也会自动选择与该员工的银行帐户关联的银行。



当然,如果用户更改了该银行帐户的国家/地区选择,则应重新加载第二个ComboBox并仅显示与新选择的国家/地区关联的银行。



所有数据都来自SQL数据库,通过Linq-To-SQL类(dbml)生成,用于表示数据库表。即我有一个在我声明为Application的私有共享变量的DataContext中定义的Bank类,Country类,BankAccount类等。我还创建了一个名为DBMain的ReadOnly属性来访问它。



到目前为止,我尝试按如下方式处理此任务:

要加载第一个Combobox与所有国家我在MainWindow_Loaded事件中放置以下代码:

 Dim lstCountries  As 列表( Of  Country)= Application.DBMain.GetCountries()
cmbBankCountry.ItemsSource = lstCountries
cmbBankCountry.DisplayMemberPath = CountryName
cmbBankCountry.SelectedValuePath = CountryID





GetCountries函数如下所示:



 公共 功能 GetCountries() 作为列表( of  Coun尝试)

Dim lstCountry 作为列表(国家/地区> =(来自c Application.DBMain.Countries
选择 c
).ToList
返回 lstCountry

结束 功能





用相应的加载第二个Combobox银行,我创建了一个事件,当第一个Combobox的选择(国家)改变时触发:



 私有  Sub  DisplayBanksForCountry()

Dim lstBanks As List( Of Bank)= Application.DBMain.GetBanks(cmbBankCountry.SelectedValue)
cmbBanks。 ItemsSource = lstBanks
cmbBanks.DisplayMemberPath = BankName
cmbBanks.SelectedValuePath = BankID

End Sub





GetBanks函数如下所示:



 公共 功能 GetBanks(inCountryID  As   Integer  As 列表( 银行)

Dim lstBanks As Lis t( of Bank)=(来自b In Application.DBMain.Banks
其中b。 CountryID = inCountryID
选择 b).ToList

返回 lstBanks

结束 功能





最后,XAML文件中两个ComboBox的定义如下:



 <   ComboBox     x:名称  =  cmbBankCountry    MinWidth   =  120    Grid.Row   =  0    Grid.Column   =  1    SelectedValue   =  {Binding Path = b .BankCountryID}    SelectionChanged   =  DisplayBanksForCountry  /  >  

< ComboBox x:名称 = cmbBanks MinWidth = 120 Grid.Row = 1 Grid.Column = 1 SelectedValue = {Binding Path = b.BankID} / >





在这个解决方案中,第一个ComboBox工作正常。它加载了所有国家/地区并自动显示员工银行帐户的国家/地区。



但是,首次加载Window时,第二个ComboBox为空。只有在我更改了第一个ComboBox的选择后才会加载它。显然,这是我要求它做的,但我假设(并希望......)当ComboBox设置为通过Binding显示帐户的国家时,将发生第一个ComboBox(cmbBankCountry)的SelectionChanged事件。不幸的是,情况并非如此。



我想通过两个ComboBox之间的绑定有一个非常优雅的方法,但我的大脑仍然连接到旧的Windows Forms,面向事件的服务方式。



提前感谢您的帮助!

解决方案

你好汤姆,





首先,你需要将你选择的值分配给某些房产,但这里似乎是绑定到一些b.BankCountryID虽然我认为它不会有任何问题





解决方案:



- >绑定国家组合框

- >在加载事件上首先你需要选择这样的国家组合框的第一项



 cmbBankCountry.SelectedIndex =  0 ; 







之后您需要绑定银行组合框,现在您已选择国家哟你将能够在国家的基础上填充银行,





//在构造函数或加载事件中



//绑定国家组合框

//选择国家组合框的第一项

//绑定银行组合框一次。


您好Ashok并感谢您提出建议,但此解决方案仍然无效。

国家/地区Combobox从DataContext获取其项目,SelectedMemberPath和SelectValuePath:

  Dim  lstCountries  As  List( Of  Country)= Application.TGVDB.GetCountries()
cmbBankCountry.ItemsSource = lstCountries
cmbBankCountry.DisplayMemberPath = CountryName
cmbBankCountry.SelectedValuePath = CountryID



及其Sel ectedValue绑定到b.BankdCountryID,这是所选BankContext的BankAccount中的Bank的Country ID。

我认为你最初选择ComboBox的索引0的想法可能有效,但它没有T。我怀疑它不起作用,因为通过绑定选择国家不会触发SelectionChanged事件。



仍在寻找一种优雅的解决方法。我必须有一个简单的方法让它工作......


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:

Dim lstCountries As List(Of Country) = Application.DBMain.GetCountries()
cmbBankCountry.ItemsSource = lstCountries
cmbBankCountry.DisplayMemberPath = "CountryName"
cmbBankCountry.SelectedValuePath = "CountryID"



The GetCountries function looks like this:

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:

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:

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:

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

解决方案

Hi tom,


First thingm you need to assign your selected value to some properties but here its seems that it is binded to some b.BankCountryID although i think it wont make any problem


Solution :

-->Bind country combobox
-->On Loaded event first of all you need to select the first item of country combobox like this

cmbBankCountry.SelectedIndex= 0;




after that you need to bind banks combobox , now as you have selected country you will be able to populate banks on the basis of country,,


//In constructor or loaded event

//Bind country combobox
//select first item of country combobox
//bind banks combobox once.


Hello Ashok and thank you for the proposal, but this solution still does not work.
The country Combobox gets its items, SelectedMemberPath and SelectValuePath from the DataContext:

Dim lstCountries As List(Of Country) = Application.TGVDB.GetCountries()
cmbBankCountry.ItemsSource = lstCountries
cmbBankCountry.DisplayMemberPath = "CountryName"
cmbBankCountry.SelectedValuePath = "CountryID"


And its SelectedValue is bonded to b.BankdCountryID, which is the Bank's Country ID in the selected BankAccount of the DataContext.
I thought that your idea of initially selecting the index 0 of the ComboBox might work, but it doesn't. I suspect that it does not work because the selection of the country by binding does not trigger the SelectionChanged event.

Still looking for an elegant way to solve it. There must me an easy way to make it work...


这篇关于绑定两个ComboBox的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆