I made this code to populate a combobox. It works perfectly, however I need beyond the text to be presented in the combo, you also need to load a kind of Value, as we do on the web where we have Name and Value. I use WPF. If I fill the combo in design time like this:
<ComboBoxItem Content="Milho" Name="M"/>
<ComboBoxItem Content="Soja" Name="S"/>
<ComboBoxItem Content="Feijão" Name="F"/>
See that the name works as a Value and I can get this value in the code behind. This is code to populate the combo at runtime:
public void CarregaComboSecagem()
{
ListaSecagens lista = new ListaSecagens();
ObservableCollection<string> listaCtg;
var prod = lista.listaSecagens();
listaCtg = new ObservableCollection<string>();
foreach (var prd in prod)
{
listaCtg.Add(prd.Umidade.ToString());
}
cbxSecagem.ItemsSource = listaCtg;
}
Prod is the listSecure:
public class ListaSecagens
{
private SiloContext contexto = new SiloContext();
public List<Secagem> listaSecagens()
{
return contexto.Secagens.ToList();
}
}
My entity Drying
[Table("Secagem")]
public class Secagem
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int IdSecagem { get; set; }
public int IdProduto { get; set; }
public decimal Umidade { get; set; }
public decimal Desconto { get; set; }
public decimal Valor_Sec { get; set; }
}
My context
public class SiloContext : DbContext
{
public SiloContext()
: base("SiloConn")
{
Database.SetInitializer<SiloContext>(null);
}
public DbSet<Produto> Produtos { get; set; }
public DbSet<Balanca> Balancas { get; set; }
public DbSet<Secagem> Secagens { get; set; }
}