VB.NET-显示通过启用Silverlight的WCF服务填充的列表框中多个选定项的值的问题

时间:2019-01-24 07:45:13

标签: vb.net linq wcf silverlight listboxitem

i。我在XAML页中利用了一个ListBox(称为“ MyList”)。此列表框从启用了Silverlight的WCF服务中成功显示了多个值(通过LINQ to SQL类从SQL Server数据库中获取其数据)。

ii。我需要捕获从ListBox中选择的多个值并处理数据。

iii。目前,即使我选择了多个值,一个简单的TextBox也仅显示ListBox中的一个值。

iv。如果我遍历SelectedItems,则TextBox仅显示服务参考中使用的记录类的名称。

v。我需要的只是所有选定项目的文本。

vi。任何帮助将不胜感激。

1。 VB记录类,由OperationContract使用:

Public Class EDRObjectListCBox
    Public SubstationCBox As String
End Class

2。启用S​​ilverlight的WCF服务:

<OperationContract()>
Public Function GetEDRObjectSubstation() As List(Of EDRObjectListCBox)
    Dim pDatabase As New UniformanceMDEDailyDataClassesDataContext()

    Dim EDRObjectList = _
        (From EDRObject _
            In pDatabase.EDRObjectLists _
            Order By EDRObject.SubstationCBox
            Select EDRObject.SubstationCBox Distinct)

    Dim pEDRSubstationCBox As New List(Of EDRObjectListCBox)

    For Each c In EDRObjectList
        pEDRSubstationCBox.Add( _
            New EDRObjectListCBox With _
            {.SubstationCBox = c.ToString})

    Next

    Return pEDRSubstationCBox.ToList()

End Function

3。 MainPage.xaml.vb代码:

Partial Public Class MainPage
    Inherits UserControl

    Private WithEvents pEDRObjectService As New EDRObjectServiceReference.EDRObjectServiceClient()

Private Sub pEDRObjectService_GetEDRObjectSubstation(sender As Object, e As 
   EDRObjectServiceReference.GetEDRObjectSubstationCompletedEventArgs) 
   Handles pEDRObjectService.GetEDRObjectSubstationCompleted

    MyList.ItemsSource = e.Result.ToList()
    MyList.DisplayMemberPath = "SubstationCBox"
    MyList.SelectedValuePath = "SubstationCBox"

End Sub 

5。列表框XAML:

<ListBox x:Name="MyList" SelectionMode="Multiple" ScrollViewer.VerticalScrollBarVisibility="Visible" HorizontalAlignment="Left" Height="140" Margin="174,221,0,0" VerticalAlignment="Top" Width="194"/>

4。执行代码时,ListBox产生结果-代码按预期工作,并且从数据库表中成功填充了ListBox:

我可以在列表框中选择多个项目

在这里,我选择了2EE-DS-01、2GG-DS-01和2H4-SP-01。

2EE-DS-01
2GG-DS-01
2H1-SS-02
2H4-SP-01
2H4-SP-02

5。我尝试过的操作(单击按钮时此代码将执行):

Private Sub ButtonDisplay_Click(sender As Object, e As RoutedEventArgs) Handles ButtonDisplay.Click

For Each i As Object In MyList.SelectedItems
        MyTextBox.Text += i.ToString() & ControlChars.NewLine

Next

'Also Tried this, but it only displays the first selected item, three times (2EE-DS-01 2EE-DS-01 2EE-DS-01):

For Each i In ListBoxBreaker.SelectedItems
    MyTextBox.Text += ListBoxBreaker.SelectedValue().ToString()

End Sub

文本框中的预期结果:

2EE-DS-01、2GG-DS-01、2H4-SP-01

实际结果:

UniformanceMDEDaily.EDRObjectServiceReference.EDRObjectListCBox

由于某种原因,它显示记录类的名称?

0 个答案:

没有答案