2017-07-29 199 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。

相关问题