组合框选定的值路径选择已更改



我是WPF和数据绑定概念的新手,我正在自学(使用此应用程序和几本书)。

我尝试过搜索,但有很多关于如何绑定它的问题(和答案),但没有关于如何引用/使用SelectedValue和SelectedValuePath:中的选定项

概述:我正在window_loaded上填充一个组合框,如下所示:

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:然后,我使用所选的OrganizationType来过滤客户端的ListView,如下所示(注意,还有其他控件也会触发RefreshOrganizationClientList())。

这就是我的问题所在。如何获取所选值和/或该选择的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链接:在以下两项中,"伙伴关系"都是从组合框中选择的,而不是"慈善"。慈善是列表中呈现给用户的第一项。

SelectedValue的内容:选定值Imgur链接

-    OrganisationType_ComboBox.SelectedValue    "Charity"   Object {String}

所选项目的内容:所选项目Imgur链接

-    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

在这里,您可以用匿名类型:的实例填充组合框

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将为您完成所有反射工作,从选定项中获取该命名属性的值,并将其填充到SelectedValue属性中。在这种情况下,应该是SelectedItem.ID

Guid selectedID = (Guid)OrganisationType_ComboBox.SelectedValue;

我猜EntOrgs.ID属于Guid类型。如果不是,用实际类型的替换。

int selectedID = (int)OrganisationType_ComboBox.SelectedValue;

或者其他什么。

相关内容

  • 没有找到相关文章

最新更新