我正在使用Entity Framework创建我的系统,我已经将对象属性绑定到a DataGrid
,每个字段都显示数据,但只有一个属性Fornecedor
,是另一个类,第一次无法显示,它只会在我打开时显示新注册窗口并关闭,我不知道为什么会这样,因为我在构造函数和 NewRegister.Closed 中使用相同的方法显示列表,让我的班级休息
public class Produto
{
public int Id { get; set; }
public string Codigo { get; set; }
public string Linha { get; set; }
public int Comprimento { get; set; }
public int Largura { get; set; }
public string Cor { get; set; }
public string CorPe { get; set; }
public int Gaveta { get; set; }
public Fornecedor Fornecedor { get; set; } // <- Fornecedor is other class
public byte[] Foto { get; set; }
public int Estoque { get; set; }
public float ValorCompra { get; set; }
public float ValorVenda { get; set; }
}
Fornecedor 类在这里:
public class Fornecedor
{
public int Id { get; set; }
public string Nome { get; set; }
public string Cidade { get; set; }
public string Estado { get; set; }
public string Email1 { get; set; }
public string Email2 { get; set; }
public string Email3 { get; set; }
public string Telefone1 { get; set; }
public string Telefone2 { get; set; }
public string Telefone3 { get; set; }
}
所以,我已经DataContext
为我的 Window 设置Binding
了DataGridTextColumns
public PageMesa()
{
InitializeComponent();
ShowList();
}
private void ShowList()
{
listMesa.ItemsSource = db.Mesas.ToList();
}
private void NewCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
MesaEditar NewRegister = new MesaEditar();
NewRegister.Closed += (s, arg) => { ShowList(); };
NewRegister.ShowDialog();
}
我的数据网格
<DataGrid Name="listMesa" Grid.Row="1" Margin="10,0" AutoGenerateColumns="False" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Id}" Width="1*" />
<DataGridTextColumn Header="Codigo" Binding="{Binding Codigo}" Width="2*" />
<DataGridTextColumn Header="Linha" Binding="{Binding Linha}" Width="2*" />
<DataGridTextColumn Header="Comprimento" Binding="{Binding Comprimento}" Width="3*" />
<DataGridTextColumn Header="Largura" Binding="{Binding Largura}" Width="3*" />
<DataGridTextColumn Header="Cor" Width="2*">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding Cor}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Cor do pé" Binding="{Binding CorPe}" Width="4*" />
<DataGridTextColumn Header="Gaveta" Binding="{Binding Gaveta}" Width="2*" />
<DataGridTextColumn Header="Fornecedor" Binding="{Binding Fornecedor.Nome}" Width="4*" />
<DataGridTextColumn Header="Estoque" Binding="{Binding Estoque}" Width="2*" />
<DataGridTextColumn Header="Valor da compra" Binding="{Binding ValorCompra, StringFormat=R$0.00}" Width="4*" />
<DataGridTextColumn Header="Valor da venda" Binding="{Binding ValorVenda, StringFormat=R$0.00}" Width="4*" />
</DataGrid.Columns>
</DataGrid>
为什么会这样?对不起,我表达不好,我不知道如何表达这个问题,请每个问题都问我