【发布时间】:2018-05-12 14:04:02
【问题描述】:
我现在在 WPF 方面有了一些经验。但是,有一个问题我想不通。在绑定列表框中添加或删除项目!我见过的所有例子都只显示了一个项目的集合。如果有人提供一个包含不止一个项目的集合示例,我将非常感激。
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;
}
}
}
}
}
obs: Listbox 2 是从下面这个方法绑定的:
public ObservableCollection<fichaProduto> carregaFichaProduto(int? fichaId)
它显示了来自 fichaProduto 类的两个项目。 fichaProdutoProdutoNome 和 fichaProdutoValor。
ListBox 1 绑定来自另一个更完整的 ObservableCollection 产品数据库查询的数据。
所以,我希望从 ListBox1 将项目添加到 ListBox2,甚至从 ListBox2 中删除。
我知道如何在 WnForms 中做到这一点,但现在不知道在 WPF 中。
谢谢!
【问题讨论】: