Alexandre Hansen
Alexandre Hansen

Reputation: 61

WPF - List Binding not loading datasource

I'm having some trouble in create a data-binded ListBox. It's not showing anything when form load. Here's my code:

FunildeVendasDTO.cs

namespace DTO
{
    public class FunildeVendasDTO
    {
        private string id;
        private string descricao;
        private string prazo;
        private string cidade;
        private string fantasia;
        private string descricao_Status;
        private int status_Orcamento;

        public string Id { get => id; set => id = value; }
        public string Descricao { get => descricao; set => descricao = value; }
        public string Prazo { get => prazo; set => prazo = value; }
        public string Cidade { get => cidade; set => cidade = value; }
        public string Fantasia { get => fantasia; set => fantasia = value; }
        public string Descricao_Status { get => descricao_Status; set => descricao_Status = value; }
        public int Status_Orcamento { get => status_Orcamento; set => status_Orcamento = value; }
    }
}

FunildeVendasBLL.cs

using System;
using DTO;
using DAL;
using System.Data;
using System.Collections.ObjectModel;

namespace BLL
{
    class FunildeVendasBLL
    {
        AcessoBancoDados bd = new AcessoBancoDados();
        readonly FunildeVendasDTO dto = new FunildeVendasDTO();

        public ObservableCollection<FunildeVendasDTO> LoadNegocios()
        {
            var negocios = new ObservableCollection<FunildeVendasDTO>();
            try
            {
                var query = "SELECT n.id, so.descricao as status_orcamento, n.status_orcamento_id, n.descricao, n.prazo, cid.uf, cid.cidade, c.fantasia FROM negocio n JOIN estabelecimento e ON e.id = n.estabelecimento_id JOIN cliente c ON c.id = e.cliente_id JOIN cidades cid ON cid.id = e.cidades_id JOIN status_orcamento so ON so.id = n.status_orcamento_id ORDER BY n.status_orcamento_id, n.id";
                bd.Conectar();
                var dtt = bd.RetDataTable(query);
                foreach (DataRow dr in dtt.Rows)
                {
                    negocios.Add(new FunildeVendasDTO { Id = dr["id"].ToString(), Descricao = dr["descricao"].ToString(), Prazo = Convert.ToDateTime(dr["prazo"]).ToString("dd/MM/yyyy"), Cidade = dr["cidade"].ToString() + " - " + dr["uf"].ToString(), Fantasia = dr["fantasia"].ToString(), Descricao_Status = dr["status_orcamento"].ToString(), Status_Orcamento = Convert.ToInt32(dr["status_orcamento_id"]) });
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            finally
            {
                bd.CloseConection();
            }

            return negocios;
        }
    }
}

Window2.xaml

<Window x:Class="GeGET.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GeGET"
        mc:Ignorable="d"
        Title="Window2" Height="450" Width="800">
    <Grid>
        <ListBox x:Name="List1" ItemsSource="{Binding Lista}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button></Button>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

Window2.xaml.cs

using System.Windows;
using DTO;
using BLL;
using System.Collections.ObjectModel;

namespace GeGET
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    /// 
    public partial class Window2 : Window
    {

        FunildeVendasDTO dto = new FunildeVendasDTO();
        FunildeVendasBLL bll = new FunildeVendasBLL();

        ObservableCollection<FunildeVendasDTO> Lista;

        public Window2()
        {
            InitializeComponent();

            Lista = bll.LoadNegocios();

        }
    }
}

Any ideas for resolving this? I'm thinking it's because the Observable collection is in another namespace. Sorry for this dumb question, I'm quite new in WPF and I'm discovering it's superpowers :)

Upvotes: 0

Views: 96

Answers (2)

mm8
mm8

Reputation: 169160

  1. Turn Lista into a public property since you can only bind to these.
  2. Set the DataContext of the window to an instance of itself since you are binding to a property of a DataContext if you don't explicitly specify a source of the binding.

    public partial class Window2 : Window
    {
        readonly FunildeVendasDTO dto = new FunildeVendasDTO();
        readonly FunildeVendasBLL bll = new FunildeVendasBLL();
    
        public ObservableCollection<FunildeVendasDTO> Lista { get; private set; }
    
        public Window2()
        {
            InitializeComponent();
            Lista = bll.LoadNegocios();
            DataContext = this;
        }
    }
    

Upvotes: 1

user3104267
user3104267

Reputation: 190

The properties you bind to have to be public... Binding generally doesn't work to fields.

Upvotes: 1

Related Questions