如何将属性的属性绑定到DataGridColumn?

时间:2013-10-05 12:23:03

标签: c# wpf entity-framework binding datagrid

我正在使用Entity Framework创建我的系统,我将对象属性绑定到DataGrid,每个字段显示数据,但只有一个属性Fornecedor,是另一个类,不能在第一次显示时,它只会显示当我打开New Register窗口并关闭时,我不知道为什么会这样做,因为我在构造函数和NewRegister.Closed中使用相同的方法显示列表,休闲我的类< / p>

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并为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

<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>

为什么会这样? 抱歉我的表情不好,我不知道如何表达这个问题,请问我每个问题

0 个答案:

没有答案