How do I fill in a combo I fill in her Value with wpf and c#

Asked

Viewed 52 times

1

I made this code to fill out a combobox. It works perfectly, but I need besides the text to be presented in the combo, 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 time design 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 fill the combo in 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 list:

public class ListaSecagens
    {
        private SiloContext contexto = new SiloContext();

        public List<Secagem> listaSecagens()
        {
            return contexto.Secagens.ToList();
        }
    }

My Drying Essence

[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; }
    }
  • what kind of prod ?

  • @Rovannlinhalis, I edited the post and put the list class

1 answer

3


Place the combo source, as the list of your objects, then you access the selected object with the property SelectedValue. Example:

In your case, as you explained in the chat, you must fill out the product combo at the Window Loaded event. When the user chooses an item, you must trigger the Selectionchanged event from the product combo, and load the Drying combo. Your code must be similar to this:

    private void Window1_Loaded(object sender, RoutedEventArgs e)
    {
        comboBoxProduto.ItemsSource = ProdutoDAO.GetProdutos(); //Seu método que retorna a List<Produto>
        comboBoxProduto.DisplayMemberPath = "Nome";
    }

    private void comboBoxProduto_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (comboBoxProduto.SelectedValue != null)
        {
            comboBoxSecagem.ItemsSource = SecagemDAO.GetSecagem(((Produto)comboBoxProduto.SelectedValue).Id);
            comboBoxSecagem.DisplayMemberPath = "Umidade";
        }
        else
            comboBoxSecagem.ItemsSource = null;
    }

Example Classes

public class Secagem
{
    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; }
}

public class SecagemDAO
{
    public static List<Secagem> GetSecagem(int idProduto)
    {
        //codigo de selecionar a lista de acordo com o produto
        //return Context.Select(...Where...) //Exemplo!!!
        return new List<Secagem>();
    }

}

public class Produto
{
    public int Id { get; set; }
    public string Nome { get; set; }

}

public class ProdutoDAO
{
    public static List<Produto> GetProdutos()
    {
        //codigo do Select dos produtos
        //return Context.Select(...Where...) //Exemplo!!!
        return new List<Produto>(); 
    }
}
  • In the above case, you filled out the combo manually. In my case, which comes from a comic book, I will have to create a kind of Key/ Value and the key I present in the combo and the value would be my "value", right?

  • edited, see if it helps

  • Something I still don’t understand, let me try to do, but I’m still a little confused

  • the source of the combo, will be a list of objects, of your class and not a string list, when you access the SelectedValue you access the object that is selected, with all its values. I edited the answer again

  • I rode before this update and in the combo did not come the correct values, type, should come: Soy, Corn, Beans and etc and came Domain.Entities.Humidity. I’m sorry, but I don’t understand anything

  • missed that part: cbxSecagem.DisplayMemberPath = "Umidade";

  • It is giving error in the vars that you created. If I remove, I can bring in the combo data, but I can’t get the value, because I did not set it.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.