Jinx88909 Jinx88909 - 9 days ago 6
Vb.net Question

Binding data to a ComboBox

I seem to have hit a roadblock with binding data to a

ComboBox
. I'm developing an app in Windows Phone 8.1 and simply all I want to do is list printers in the
ComboBox
.

I've had a Google and it doesn't matter what I try I seem to be missing something as all I can list is a load of blank values.

This is my XAML:

<ComboBox x:Name="cbxPrinters"
HorizontalAlignment="Left"
Margin="153,150,0,0"
VerticalAlignment="Top"
Width="237">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="2">
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>


This is the VB class that populates the list (the data is being returned as
Id|Name|Id|Name|Id|Name etc
which is why there is a split and a
For Next
loop) however this bit of code does populate the printer class correctly and correctly adds to the list. At the end I'm left with a list of 54 printers:

Public Class Printer

Public Id As Integer
Public Name As String

Public Function Populate(ByVal ResultString As String) As List(Of Printer)

Dim _Results As List(Of String) = ResultString.Split(New String("|")).ToList

Dim _Printers As New List(Of Printer)

If _Results.Count >= 2 Then

For i = 0 To _Results.Count - 1 Step 2
Dim _Printer As New Printer

With _Printer
.Id = _Results(i)
.Name = _Results(i + 1).Trim()
End With

_Printers.Add(_Printer)
Next
End If

Return _Printers

End Function

End Class


This bit of code assigns the
List(Of Printer)
to the
ComboBox
:

Protected Overrides Async Sub OnNavigatedTo(e As NavigationEventArgs)

_Printer = New Printer

_Printers = _Printer.Populate(Await GetPrinterList())

If _Printers.Count > 0 Then
cbxPrinters.ItemsSource = _Printers
End If

End Sub


When I launch the app and come to this page the code runs OK but all I'm left with is what looks like 54 blank options to select from.

Any ideas at all from someone who has dealt with this control before? Am I missing something?

Answer

OK, managed to fix the issue with the help of @Zaggler and after investigating the stack trace:

Error: BindingExpression path error: 'Id' property not found on 'COACH_HOUSE_SCAN_APP.Printer, COACH HOUSE SCAN APP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.`

I changed the class slightly so that Id and Name are now Properties:

Public Property Id As Integer
Public Property Name As String

I also remove this part of the XAML:

<ComboBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Vertical" Margin="2">
            <TextBlock Text="{Binding Name}"></TextBlock>
        </StackPanel>
    </DataTemplate>
</ComboBox.ItemTemplate>

This has fixed my issue and now the printers are listed in the ComboBox.

Comments