我有ComboBox
<ComboBox Grid.Column="1" Grid.Row="1" Margin="3" Name="cmbPlayer1" IsEditable="true"></ComboBox>
我用数据库中的数据初始化我的Combobox。我有程序初始化这个,然后我调用这个程序。
Public Sub InitComboxesPlayers(cmbPlayer As ComboBox)
Using myDataReader As SqlDataReader = GetSqlFunctions.GetExecutedDataReaderFromSql(
"SELECT PLAYER_ID, " &
" PLAYER_NICKNAME, " &
" PLAYER_FIRSTNAME, " &
" PLAYER_LASTNAME " &
" FROM PLAYERS ", myConnection)
While myDataReader.Read
Dim myNewPlayer As New Players
With myNewPlayer
.Player_ID = CInt(myDataReader("PLAYER_ID"))
.Nickname = myDataReader("PLAYER_NICKNAME").ToString.Trim
.Firstname = myDataReader("PLAYER_FIRSTNAME").ToString.Trim
.Lastname = myDataReader("PLAYER_LASTNAME").ToString.Trim
End With
lstOfPlayers.Add(myNewPlayer)
End While
End Using
cmbPlayer.ItemsSource = lstOfPlayers
cmbPlayer.DataContext = lstOfPlayers
cmbPlayer.DisplayMemberPath = "PLAYER_NICKNAME"
cmbPlayer.SelectedValuePath = "PLAYER_ID"
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
InitComboxesPlayers(cmbPlayer1)
End Sub
因此,我的值不会显示在组合框中。但你可以在图片上看到值在Combobox中,因为它不是空的。怎么了?
答案 0 :(得分:0)
试试这个......您需要 分配您的类 中的属性名称,而不是来自您的SQL的列名......
cmbPlayer.DisplayMemberPath = "Nickname"
cmbPlayer.SelectedValuePath = "Player_ID"
答案 1 :(得分:-1)
两种方法: -
1)更新代码以使用属性而不是DataBase列名: -
cmbPlayer.DisplayMemberPath = "Nickname "
2)您可以为组合框创建项目模板,因此您无需在代码中设置DisplayMemberPath。
<ComboBox Grid.Column="1" Grid.Row="1" Margin="3" Name="cmbPlayer1" IsEditable="true">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text={Binding Nickname}
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>