2016-06-06 3 views
0

Ich bin neu in WPF und das Konzept der Datenbindung, und ich bin dabei, mich selbst zu unterrichten (mit dieser Anwendung und ein paar Bücher).ComboBox SelectedValuePath SelectionChanged

ich gesucht habe es versucht, aber es gibt viele Fragen (und Antworten) darüber, wie es zu binden, aber nicht, wie/referenzieren das ausgewählte Element aus den SelectedValue und SelectedValuePath verwenden:

Übersicht: I bin Bestücken eine ComboBox auf, wie unten 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: Ich verwende dann den ausgewählten OrganizationType, um eine ListView von Clients wie unten zu filtern (Beachten Sie, dass andere Steuerelemente auch RefreshOrganisationClientList() auslösen).

Hier liegt mein Problem. Wie bekomme ich den ausgewählten Wert und/oder die ID (SelectedValuePath) für diese Auswahl.

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 

Ich fange ich bin Missverständnis, das Konzept Datenbindung selbst zu denken und wie die Daten von Kontrollen ausgewählt verwenden, die Daten gebunden sind, oder ich habe etwas einfach übersehen.

EDIT: Ich habe versucht, ein Bild zu posten, aber da ich neu hier bin, bin ich dazu nicht in der Lage, also eine Beschreibung und einen imgur Link zu tun haben: In den beiden unten genannten "Partnerschaften" war ausgewählt aus der ComboBox nicht "Charity". Nächstenliebe ist der erste Punkt in der Liste, der dem Benutzer präsentiert wird.

Inhalt von SelectedValue: SelectedValue Imgur Link

- OrganisationType_ComboBox.SelectedValue "Charity" Object {String} 

Inhalt von 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 

EDIT: Für alle, die später über diese stolpert; Das Folgende ist das Update, um das obige Problem zu beheben. Thanks again !:

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 

Antwort

0

Hier legen Sie füllen Ihre Combobox mit Instanzen eines anonymen Typ:

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 

Wenn die Benutzer wählt etwas aus, ComboBox.SelectedItem wird Eine Instanz dieses anonymen Typs sein. Sie benötigen ein paar Zeilen mit Reflektionscode, um alle Eigenschaftswerte daraus zu erhalten, da Sie sie nicht in irgendetwas umwandeln können.

Zum Glück hatten Sie den guten Sinn, SelectedValuePath="ID" auf der Combobox zu setzen. Wenn SelectedValuePath eine Eigenschaft benennt, die tatsächlich auf dem ausgewählten Element vorhanden ist, wird ComboBox all diese Reflektion für Sie arbeiten, den Wert dieser benannten Eigenschaft aus dem ausgewählten Element abrufen und in die Eigenschaft SelectedValue einfügen.In diesem Fall sollte das SelectedItem.ID sein.

Guid selectedID = (Guid)OrganisationType_ComboBox.SelectedValue; 

Ich vermute hier, dass EntOrgs.ID vom Typ Guid. Wenn es nicht ist, ersetzen Sie jeden Typ, der es tatsächlich ist.

int selectedID = (int)OrganisationType_ComboBox.SelectedValue; 

Oder was auch immer.

+0

Ja, die ID ist eine GUID. OrganizationType_ComboBox.SelectedValue gibt die Textbeschreibung (OrganizationType) des ersten Elements in der ComboBox nicht des ausgewählten Elements oder der ID für das ausgewählte Element zurück. –

+0

Versuchen Sie 'SelectedValue = "{Binding OrganizationType}" 'aus dem XAML zu entfernen, das habe ich nicht bemerkt. –

+0

Yep fantastisch, das hat funktioniert! Das von mir verwendete Beispiel hat SelectedValue = "{Binding *****}". Jetzt schaue ich es wieder an, ist das nur sagen, dass unabhängig davon, was ausgewählt ist, die gesamte gebundene Liste zurückgeben, wenn gefragt wird, was der SelectedValue ist? Wenn ja, kann ich nicht sehen, warum das jemals benutzt wurde? –