在创建数据绑定的ListBox时遇到一些麻烦。加载表单时未显示任何内容。这是我的代码:
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();
}
}
}
解决这个问题有什么想法吗? 我想这是因为Observable集合在另一个名称空间中。 对不起,这个愚蠢的问题,我在WPF中还很陌生,我发现它是超级大国:)
答案 0 :(得分:1)
您绑定的属性必须是公开的... 绑定通常不适用于字段。
答案 1 :(得分:1)
Lista
设置为公共属性,因为您只能绑定到这些属性。将窗口的DataContext
设置为其自身的实例,因为如果您未明确指定绑定的源,则绑定到DataContext
的属性。>
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;
}
}