我編寫了一個通過ConnectionString與SQL Anywhere數據庫建立連接的工具。 我想要接收一些數據並通過Loaded Event將它們綁定到ComboBox。C# - 組合框中的數據翻倍(SQLAnywhere連接)
private void VesselNoComboBox_Loaded(object sender, RoutedEventArgs e) { ListShipNames(); }
/// <summary>
/// Receive all vessels from Database and add them to the combobox.
/// A variable Database connection has to be built.
/// </summary>
private void ListShipNames()
{
SAConnection sqlCon = new SAConnection();
sqlCon.ConnectionString = "Data Source=****; User id=****; Password=****";
try
{
sqlCon.Open();
SACommand cmd = new SACommand("Select ID || ' ' || NAME from test.SHIP_DATA", sqlCon);
SADataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
VesselNoComboBox.Items.Add(reader.GetString(0));
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error ", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
finally
{
sqlCon.Close();
}
}
在WPF啓動程序當所述第一ComboBox_Loaded將被執行。然後當UserControl將被加載。所有數據都會翻倍。明確的Clear()
將刪除組合框條目。 有人可以給我一個提示嗎?
您是通過模板還是通過應用模板中的代碼加載控件? – 2014-11-03 22:21:51
不,我直接在我的WPF窗口中加載UserControl。如: –
2014-11-04 15:58:37
xmlns:tabData="clr-namespace:xxx.UserControls"
並參考它