Fill textbox with combobox

1

I'm trying to fill TextBox from a% of ComboBox , my problem is that I show the data of ComboBox and fill the TextBox but when I change the item in ComboBox the data of TextBox remain static.

I share the code.

C #:

/*PROCEDIMIENTO PARA CARGAR LOS PRODUCTOS CON SU CODIGO PSL Y TOTALIZADOR*/
        private void ListarProductos()
        {
            using(DB_SICAPEntities entidades = new DB_SICAPEntities())
            {
                var query = from i in entidades.tblItems
                            join t in entidades.tblItemsTotalizador
                            on i.idTotalizador equals t.id
                            where i.Activo == true
                            select new
                            {
                                i.Item,
                                i.CodigoPSL,
                                t.CodigoTotalizador
                            };

                foreach(var result in query)
                {
                    productos.Add(new tblItems { Item = result.Item, CodigoPSL = result.CodigoPSL });
                    txtTotalizador.Text = result.CodigoTotalizador;
                    txtPsl.Text = Convert.ToString(result.CodigoPSL);
                }
                productos.Add(new tblItems { id = int.Parse("-1"), Item = "Productos" });
                this.cmbProductos.DisplayMemberPath = "Item";
                this.cmbProductos.SelectedValuePath = "id";
                this.cmbProductos.ItemsSource = productos.OrderBy(P => (P.id)).ToList();
                this.cmbProductos.SelectedValue = "-1";
            }

        }

XAML:

<Label Content="Seleccionar Producto" HorizontalAlignment="Left" Margin="27,52,0,0" VerticalAlignment="Top" Height="30" RenderTransformOrigin="-1.132,4.423" Width="133" FontSize="12" Foreground="#FF5B5B5B" FontWeight="SemiBold"/>
        <ComboBox x:Name="cmbProductos" HorizontalAlignment="Left" Margin="27,82,0,0" VerticalAlignment="Top" Width="198" Height="23"/>
        <Label Content="Código PSL" HorizontalAlignment="Left" Margin="271,52,0,0" VerticalAlignment="Top" Height="30" RenderTransformOrigin="-1.132,4.423" Width="133" FontSize="12" Foreground="#FF5B5B5B" FontWeight="SemiBold"/>
        <TextBox x:Name="txtPsl" HorizontalAlignment="Left" Cursor="Help" Height="23" Margin="271,82,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="186" ToolTip="No puedes modificar este campo" Foreground="White" Background="Gray"/>
        <Label Content="Código Totalizador" HorizontalAlignment="Left" Margin="39,52,0,0" VerticalAlignment="Top" Height="30" RenderTransformOrigin="-1.132,4.423" Width="133" FontSize="12" Foreground="#FF5B5B5B" FontWeight="SemiBold" Grid.Column="1"/>
        <TextBox x:Name="txtTotalizador" HorizontalAlignment="Left" Cursor="Help" Height="23" Margin="39,82,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="186" Grid.Column="1" ToolTip="No puedes modificar este campo" Foreground="White" Background="Gray"/>

Thank you very much in advance for your contributions.

    
asked by Brian Velez 03.12.2018 в 19:20
source

1 answer

1

The first thing I would recommend is that you do not need to perform a foreach to convert linq object, you can do everything in the same action

public class Item
{
   public int Id {get;set;}
   public string Item {get;set;}
   public string CodigoPSL {get;set;}
   public string CodigoTotalizador {get;set;}
}

private void ListarProductos()
{
    using(DB_SICAPEntities entidades = new DB_SICAPEntities())
    {
        var query = (from i in entidades.tblItems
                    join t in entidades.tblItemsTotalizador on i.idTotalizador equals t.id
                    where i.Activo == true
                    orderby i.id
                    select new Item()
                    {
                        Id = i.id,
                        Item = i.Item,
                        CodigoPSL = i.CodigoPSL,
                        CodigoTotalizador = t.CodigoTotalizador
                    }).ToList();


        query.Insert(0, new tblItems { Id= -1, Item = "Productos" });

        this.cmbProductos.DisplayMemberPath = "Item";
        this.cmbProductos.SelectedValuePath = "id";
        this.cmbProductos.ItemsSource = query;
        this.cmbProductos.SelectedValue = "-1";
    }

}

You will see in the select how the class you want to get is defined and also apply order and filter all in one

Then in the xaml you must assign the event SelectionChanged

<ComboBox x:Name="cmbProductos" SelectionChanged="cmbProductos_SelectionChanged" HorizontalAlignment="Left" Margin="27,82,0,0" VerticalAlignment="Top" Width="198" Height="23"/>

In order to have the selected item and load the textbox

private void cmbProductos_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var comboBox = sender as ComboBox;

    var item = comboBox.SelectedItem as tblItems;

    txtTotalizador.Text = item.CodigoTotalizador;
    txtPsl.Text = item.CodigoPSL;
}

You can get the class assigned as datasource and take the properties. Validate the properties of the class tblItems

    
answered by 03.12.2018 / 21:18
source