2017-07-29 200 views
0

我在WPF中獲得了一些經驗。但是,有一個問題我無法弄清楚。添加或刪除項目到/從綁定列表框!我見過的所有例子都只顯示了一個單一項目的收集。如果有人提供一個具有多個單個項目的集合的例子,我會非常感激。將列表框1中的項目添加到綁定多項列表框2

interface DbInterface 
{ 

    ObservableCollection<fichaProduto> carregaFichaProduto(int? fichaId); 

} 
    public class fichaProduto 
{ 
    public int fichaProdutoFichaId { get; set; } 
    public int fichaProdutoProdutoId {get;set;} 
    public string fichaProdutoProdutoNome { get; set; } 
    public string fichaProdutoStatusId { get; set; } 
    public string fichaProdutoStatusNome { get; set; } 
    public string fichaProdutoOrdemServico { get; set; } 
    public DateTime? fichaProdutoDataInstalacao { get; set; } 
    public string fichaProdutoHorarioDe { get; set; } 
    public string fichaProdutoHorarioAte { get; set; } 
    public string fichaProdutoContrato { get; set; } 
    public string fichaProdutoCodigoInstalacao { get; set; } 
    public Decimal fichaProdutoValor { get; set; } 
} 
    class dbMysql : DbInterface 
    { 
    public ObservableCollection<fichaProduto> carregaFichaProduto(int? fichaId) 
    { 
     using (MySqlConnection smartConexaoDb = new MySqlConnection(smartSingleTon.connectionStringDb)) 
     { 
      using (MySqlCommand cmd = new MySqlCommand("SELECT fc.id,pp.nome,st.nome,fp.os,fp.dia_instalacao,fp.horarioDe,fp.horarioAte,fp.contrato,fp.codigo_instalacao,fp.valor FROM ficha fc JOIN ficha_produto fp ON fc.id = fp.id_ficha LEFT JOIN ficha_produto fp2 ON (fc.id = fp2.id_ficha AND fp.id < fp2.id) JOIN produto_plano pp ON pp.id = fp.id_produto JOIN status st ON fp.id_status = st.id WHERE fc.id = @fichaId ORDER BY fc.id DESC", smartConexaoDb)) 
      { 
       cmd.Parameters.AddWithValue("@fichaId", fichaId); 
       smartConexaoDb.Open(); 

       using (MySqlDataReader dr = cmd.ExecuteReader()) 
       { 
        var listFichaProduto = new ObservableCollection<fichaProduto>(); 

        while (dr.Read()) 
        { 
         listFichaProduto.Add 
          (new fichaProduto() 
          { 
           fichaProdutoFichaId = Convert.ToInt32(dr.GetValue(0)), 
           fichaProdutoProdutoNome = Convert.ToString(dr.GetValue(1)), 
           fichaProdutoStatusNome = Convert.ToString(dr.GetValue(2)), 
           fichaProdutoOrdemServico = Convert.ToString(dr.GetValue(3)), 
           fichaProdutoHorarioDe = Convert.ToString(dr.GetValue(5)), 
           fichaProdutoHorarioAte = Convert.ToString(dr.GetValue(6)), 
           fichaProdutoContrato = Convert.ToString(dr.GetValue(7)), 
           fichaProdutoCodigoInstalacao = Convert.ToString(dr.GetValue(8)), 
           fichaProdutoValor = Convert.ToDecimal(dr.GetValue(9)) 
          }); 
        } 
        return listFichaProduto; 
       } 
      } 
     } 
    } 
    } 

觀測值:列表框2從下面這種方法結合:

public ObservableCollection<fichaProduto> carregaFichaProduto(int? fichaId) 

它示出了從fichaProduto類兩個項目就可以了。 fichaProdutoProdutoNome和fichaProdutoValor。

ListBox 1從另一個更完整的ObservableCollection產品數據庫查詢綁定數據。

所以,從ListBox1所有我想要的是添加項目到ListBox2甚至刪除然後從ListBox2。

我知道如何在WnForms中做到這一點,但現在不知道如何在WPF中做到這一點。

謝謝!

回答

0

很好,因爲沒有人能夠解決它,導致網上沒有關於如何做的信息,讓我們回到WinForms的方式來解決它。所以我要做的是加載一個新的SQl查詢來更新我的ObservableCollection列表並將其重新返回給ItemsSource。

相關問題