我是WPF和数据绑定概念的新手,我正在自学(使用此应用程序和一些书籍)。
我尝试过搜索,但是有很多关于如何绑定它的问题(和答案),而不是如何引用/使用SelectedValue和SelectedValuePath中的选定项目:
概述: 我正在window_loaded上填充一个ComboBox,如下所示:
Private db As New Pluto_DBDataContext()
Private OrganisationTypeView As BindingListCollectionView
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
LoadOrganisationType()
End Sub
Private Sub LoadOrganisationType()
Dim OrganisationTypeList = From EntOrgs In db.t_EntityOrgs
Join EntType In db.t_EntityTypes On EntType.ID Equals EntOrgs.FK_EntityType_ID
Order By EntOrgs.OrganisationType
Where EntOrgs.DateTime_To Is Nothing AndAlso EntType.DateTime_To Is Nothing _
AndAlso ((Not bolIndividualEntityType) And EntType.EntityType <> "Individual")
Select EntOrgs.OrganisationType, EntOrgs.ID
OrganisationType_ComboBox.DataContext = OrganisationTypeList
Me.OrganisationTypeView = CType(CollectionViewSource.GetDefaultView(OrganisationType_ComboBox.DataContext), BindingListCollectionView)
End Sub
XAML:
<ComboBox x:Name="OrganisationType_ComboBox" Grid.Row="1" Grid.Column="0" ItemsSource="{Binding}" SelectedValue="{Binding OrganisationType}" SelectedValuePath="ID" DisplayMemberPath="OrganisationType"/>
ComboBox_SelectionChanged: 然后我使用选定的OrganisationType过滤客户端的ListView,如下所示(注意还有其他控件也会触发RefreshOrganisationClientList())。
这就是我的问题所在。如何获取所选值和/或该选择的ID(SelectedValuePath)。
Private Sub OrganisationType_ComboBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles OrganisationType_ComboBox.SelectionChanged
If OrganisationType_ComboBox.SelectedItem IsNot Nothing Then
RefreshOrganisationClientList()
End If
End Sub
Private Sub RefreshOrganisationClientList()
Dim sOrgType As String
Dim guOrgType_ID As Guid
Dim sOrganisationName As String
Dim sPostCode As String
Dim sOccupation As String
If OrganisationType_ComboBox.SelectedItem Is Nothing Then
sOrgType = Nothing
Else
'*****PROBLEM HERE*****
sOrgType = OrganisationType_ComboBox.SelectedItem.index(1).ToString
guOrgType_ID = OrganisationType_ComboBox.SelectedItem
'guOrgType_ID = Guid.Parse(OrganisationType_ComboBox.SelectedValuePath)
End If
MsgBox(sOrgType)
MsgBox(guOrgType_ID)
sOrganisationName = OrganisationName_TextBox.Text
sPostCode = OrgPostalCode_TextBox.Text
sOccupation = OrgOccupation_TextBox.Text
Dim FilteredClientList = From Clients In db.t_Clients
Join EntType In db.t_EntityTypes On EntType.ID Equals Clients.FK_EntityType_ID
Join EntOrgs In db.t_EntityOrgs On EntType.ID Equals EntOrgs.FK_EntityType_ID
Order By Clients.OrganisationName, Clients.Occupation, EntOrgs.OrganisationType
Where Clients.DateTime_To Is Nothing AndAlso EntType.DateTime_To Is Nothing AndAlso EntOrgs.DateTime_To Is Nothing _
AndAlso ((Not bolIndividualEntityType) And EntType.EntityType <> "Individual") _
AndAlso (If(sOrgType IsNot Nothing, EntOrgs.ID = guOrgType_ID, True)) _
AndAlso (If(sOrganisationName IsNot "", Clients.OrganisationName Like "*" & sOrganisationName & "*", True)) _
AndAlso (If(sPostCode IsNot "", Clients.Postal_Code Like "*" & sPostCode & "*", True)) _
AndAlso (If(sOccupation IsNot "", Clients.Occupation Like "*" & sOccupation & "*", True))
Select Clients.OrganisationName, EntOrgs.OrganisationType, Clients.Occupation, Clients.FormationDate, Clients.Postal_Code, Clients.ID
ExistingOrganisationClients_ListView.DataContext = FilteredClientList
Me.FilteredOrgansiationClientView = CType(CollectionViewSource.GetDefaultView(ExistingOrganisationClients_ListView.DataContext), BindingListCollectionView)
End Sub
我开始认为我误解了数据绑定本身的概念以及如何使用从数据绑定的控件中选择的数据,或者我忽略了一些简单的事情。
编辑:我试图发布图片,但由于我是新来的,我无法这样做,所以描述和imgur链接必须这样做: 在下面的两个“伙伴关系”中选择了ComboBox而不是“Charity”。慈善是提交给用户的列表中的第一项。SelectedValue的内容: SelectedValue Imgur Link
- OrganisationType_ComboBox.SelectedValue "Charity" Object {String}
SelectedItem的内容: SelectedItem Imgur Link
- OrganisationType_ComboBox.SelectedItem OrganisationType="Partnerships", ID={27775e86-0013-4b82-996f-f6c061e99b2f} Object {VB$AnonymousType_3(Of String, System.Guid)}
+ ID {27775e86-0013-4b82-996f-f6c061e99b2f} System.Guid
- OrganisationType "Partnerships" String
编辑:对于后来偶然发现的人;以下是更新以解决上述问题。再次感谢!:
XAML:
<ComboBox x:Name="OrganisationType_ComboBox" Grid.Row="1" Grid.Column="0" ItemsSource="{Binding}" SelectedValuePath="ID" DisplayMemberPath="OrganisationType"/>
ComboBox_SelectionChanged:
Private Sub RefreshOrganisationClientList()
Dim sOrgType As String
Dim guOrgType_ID As Guid
Dim sOrganisationName As String
Dim sPostCode As String
Dim sOccupation As String
If OrganisationType_ComboBox.SelectedItem Is Nothing Then
guOrgType_ID = Nothing
Else
guOrgType_ID = OrganisationType_ComboBox.SelectedValue
End If
sOrganisationName = OrganisationName_TextBox.Text
sPostCode = OrgPostalCode_TextBox.Text
sOccupation = OrgOccupation_TextBox.Text
Dim FilteredClientList = From Clients In db.t_Clients
Join EntType In db.t_EntityTypes On EntType.ID Equals Clients.FK_EntityType_ID
Join EntOrgs In db.t_EntityOrgs On EntType.ID Equals EntOrgs.FK_EntityType_ID
Order By Clients.OrganisationName, Clients.Occupation, EntOrgs.OrganisationType
Where Clients.DateTime_To Is Nothing AndAlso EntType.DateTime_To Is Nothing AndAlso EntOrgs.DateTime_To Is Nothing _
AndAlso ((Not bolIndividualEntityType) And EntType.EntityType <> "Individual") _
AndAlso (If(guOrgType_ID <> Guid.Empty, EntOrgs.ID = guOrgType_ID, True)) _
AndAlso (If(sOrganisationName IsNot "", Clients.OrganisationName Like "*" & sOrganisationName & "*", True)) _
AndAlso (If(sPostCode IsNot "", Clients.Postal_Code Like "*" & sPostCode & "*", True)) _
AndAlso (If(sOccupation IsNot "", Clients.Occupation Like "*" & sOccupation & "*", True))
Select Clients.OrganisationName, EntOrgs.OrganisationType, Clients.Occupation, Clients.FormationDate, Clients.Postal_Code, Clients.ID
ExistingOrganisationClients_ListView.DataContext = FilteredClientList
Me.FilteredOrgansiationClientView = CType(CollectionViewSource.GetDefaultView(ExistingOrganisationClients_ListView.DataContext), BindingListCollectionView)
End Sub
答案 0 :(得分:0)
在这里,您使用匿名类型的实例填充组合框:
Dim OrganisationTypeList = From EntOrgs In db.t_EntityOrgs
Join EntType In db.t_EntityTypes On EntType.ID Equals EntOrgs.FK_EntityType_ID
Order By EntOrgs.OrganisationType
Where EntOrgs.DateTime_To Is Nothing AndAlso EntType.DateTime_To Is Nothing _
AndAlso ((Not bolIndividualEntityType) And EntType.EntityType <> "Individual")
Select EntOrgs.OrganisationType, EntOrgs.ID
OrganisationType_ComboBox.DataContext = OrganisationTypeList
当用户选择某个内容时,ComboBox.SelectedItem
将是该匿名类型的实例。您需要使用几行反射代码来获取任何属性值,因为您无法将其转换为任何属性值。
幸运的是,你有理由在组合框上设置SelectedValuePath="ID"
。如果SelectedValuePath
命名了所选项目上实际存在的属性,ComboBox
将为您完成所有反射工作,从所选项目中获取该命名属性的值,并将其填入{ {1}}财产。在这种情况下,应该是SelectedValue
。
SelectedItem.ID
我猜这里Guid selectedID = (Guid)OrganisationType_ComboBox.SelectedValue;
的类型为EntOrgs.ID
。如果不是,则替换实际的任何类型。
Guid
或者其他什么。