I have a strange issue when binding a combobox in a WPF window.
I am loading a List object, to populate the combobox, through it’s .ItemsSource, on Window Load, however, it does not populate.
At least, not until the arrow is clicked on. Once the arrow is clicked on, the window freezes up, and after a minute or so, the 4000+ records are displayed in it.
Now, the list gets loaded, however, when I click on the combobox it takes awhile for it to render, so how can I show a waitcursor while it is rendering the list?
Here’s the Xaml for it:
<ComboBox Grid.Column="1"
HorizontalAlignment="Stretch" Margin="3" Name="tUser" VerticalAlignment="Stretch"
DisplayMemberPath="UsersName" SelectedValuePath="UserID" SelectedValue="0"/>
And the code-behind is:
Private Sub CreateTask_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Using New WaitCursor
_LA.Show()
Me.tOrder.ItemsSource = GetOrderList()
Me.tUser.ItemsSource = GetUserList()
_LA.Hide()
End Using
End Sub
Private Function GetUserList() As List(Of UserTyping)
Dim _Qry As New List(Of UserTyping)
Using db As New DataAccess
With db
.QueryType = CmdType.InlineSQL
.Query = "Select 0 As UserID, '-Select User-' As UsersName Union All Select userID As UserID, userFullName As UsersName From vwSelectUser"
Using _Results = .GetResults()
If _Results IsNot Nothing Then
If _Results IsNot Nothing Then
_Qry = (From row In _Results.Cast(Of DbDataRecord)()
Select New UserTyping() With {
.UserID = Common.IsNull(Of Long)(row, 0, 0),
.UsersName = Common.IsNull(Of String)(row, 1, String.Empty)
}).ToList()
Else
_Qry = New List(Of UserTyping)
End If
End If
End Using
End With
End Using
Return _Qry
_Qry.Clear()
End Function
Partial Public Class UserTyping
Public Property UserID As Long
Public Property UsersName As String
End Class
I’m not sure if there’s a better way, but…
If you want to avoid waiting when you open the
ComboBoxfor the first time when it has many items, you can setComboBox.IsDropDownOpentoTrueand back toFalse.Edit:
A little bit of research turned up VirtualizingStackPanel. Basically, your XAML will look like this:
I was able to view a
ComboBoxwith 40k numbers instantly.