Lucho Mansilla
Lucho Mansilla

Reputation: 31

XAML Binding issues

I have spent the last three days trying to make this work, I don't really know how to do this, in short:

 <Grid DataContext="{Binding Path=Transaccion}">
    <DataGrid Name="tr" AutoGenerateColumns="False" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Fecha" Binding="{Binding Path=Transaccion.Fecha}" />
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="120"/>
                        <ColumnDefinition Width="70"/>
                        <ColumnDefinition Width="70"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Path=SubRubro.Nombre}"/>
                    <TextBlock Grid.Column="1" Text="{Binding Path=Monto}"/>
                    <TextBlock Grid.Column="2" Text="{Binding Path=TipoTransaccion}"/>
                </Grid>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>
</Grid>

Transacciones POCO has primary key which is referenced by DetallesTransaccion(Transaction details) in his Transaccion_Id column, this XAML I want all the references DetallesTransaccion referencing the same Transaccion_Id appearing in the rowDetails, now is appearing only one per Transaccion. Then I have another issue, that is related to data binding too and I cant get it to work either:

[Table("SubRubro")]
public class SubRubro
{  
    public int Id { get; set; }

    public string Codigo { get; set; }

    public string Nombre { get; set; }        

    public int? ParentId { get; set; }        

    public virtual SubRubro Parent { get; set; }
}

ALL SubRubros have a self-reference to the table in the parentId column, all SubRubros have a direct parent, I cant make a visual representation of this I tried everything, HierarchicalDataTemplate, DataTemplate, here is a example of rows in my table:

ID   CODE       NAME             PARENTID

1   10.1      SubRubro 0           NULL(Dont have parent)
21  10.1.1    SubRubro anidado 0    1
22  10.1.2    SubRubro anidado 1    1
23  10.1.3    SubRubro anidado 2    1
24  10.1.1.1  SubRubro anidado 3    21
25  10.1.1.2  SubRubro anidado 4    21

Can you enlighten me?

EDIT This is my Transactions class:

 public partial class Transaccion
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Transaccion()
    {
        DetallesTransaccion = new HashSet<DetallesTransaccion>();
    }

    public int Id { get; set; }

    [Column(TypeName = "date")]
    public DateTime Fecha { get; set; }

    [StringLength(50)]
    public string Descripcion { get; set; }

    public int AutorizaId { get; set; }

    public int ConfeccionaId { get; set; }

    public int CentroCostoId { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<DetallesTransaccion> DetallesTransaccion { get; set; }

    public virtual Usuario Autoriza { get; set; }

    public virtual Usuario Confecciona { get; set; }
}

Transaction Details:

public partial class DetallesTransaccion
{
    public int Id { get; set; }

    public int TransaccionId { get; set; }

    public int SubRubroId { get; set; }

    public decimal? Monto { get; set; }

    public TipoTransaccion TipoTransaccion { get; set; }

    public virtual SubRubro SubRubro { get; set; }

    public virtual Transaccion Transaccion { get; set; }
}

public enum TipoTransaccion
{
    Debita = 0,
    Acredita = 1
}

Viewmodel:(for testing)

    private NeotekDB ctx = new NeotekDB();

    public List<DetallesTransaccion> Transaccion
    {
        get { return ctx.DetallesTransaccion.Include("Transaccion").ToList(); }
    }

    public List<Transaccion> Detalles
    {
        get { return ctx.Transacciones.Include("DetallesTransaccion").ToList(); }
    } 

Both queries are the same but differente entry points, trying to see if there is difference, but no. EDIT 2 I am getting this:THIS

I want this: Want this

Upvotes: 0

Views: 254

Answers (1)

Rachel
Rachel

Reputation: 132548

The first step would be to parse your data into a set of objects that accurately represents your data hierarchy.

Depending on what kind of behavior or appearance you want, there's two ways of doing this.

Either parse into separate Transaction and TransactionDetail objects, like below :

public class Transaction
{
    // all properties related to Transaction
    public int TransaccionId { get; set; }
    public DateTime Fecha { get; set; }
    public string Descripcion { get; set; }
    public int AutorizaId { get; set; }
    public int ConfeccionaId { get; set; }
    public int CentroCostoId { get; set; }
    public Usuario Autoriza { get; set; }
    public Usuario Confecciona { get; set; }

    // A list of the transaction detail objects for this transaction
    public List<TransactionDetail> TransactionDetails { get; set; }
}

public class TransactionDetail
{
    // all properties related to transaction detail record
    public int TransactionDetailId { get; set; }
    public decimal? Monto { get; set; }
    public TipoTransaccion TipoTransaccion { get; set; }
    public string Codigo { get; set; }
    public string Nombre { get; set; }  

    // this could also be a Transaction object if needed
    public int ParentTransactionId { get; set; }
}

And draw your List<Transactions> with a DataGrid using a DataGridTemplateColumn to draw the the list of TransactionDetails however you want (ItemsControl, ListBox, etc).

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Transactions}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="TransaccionId" Binding="{Binding TransaccionId}" />
        <DataGridTextColumn Header="Fecha" Binding="{Binding Fecha}" />
        <DataGridTextColumn Header="Descripcion" Binding="{Binding Descripcion}" />

        <DataGridTemplateColumn Header="Details">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ListBox ItemsSource="{Binding TransactionDetails}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

Or alternatively flatten your objects so each object contains the full set of both TransactionDetail and Transaction data, and set your DataGrid's grouping to group based on TransactionId.

Your data objects for the grid would then look something like this :

public class TransactionDetail
{
    // all properties related to Transaction
    public int TransaccionId { get; set; }
    public DateTime Fecha { get; set; }
    public string Descripcion { get; set; }
    public int AutorizaId { get; set; }
    public int ConfeccionaId { get; set; }
    public int CentroCostoId { get; set; }
    public Usuario Autoriza { get; set; }
    public Usuario Confecciona { get; set; }

    // all properties related to transaction detail record
    public int TransactionDetailId { get; set; }
    public int ParentTransactionId { get; set; }
    public decimal? Monto { get; set; }
    public TipoTransaccion TipoTransaccion { get; set; }
    public string Codigo { get; set; }
    public string Nombre { get; set; }  
}

And your XAML would probably look something similar to this MSDN example. (Sorry, don't feel like writing it all out here. There's a lot of examples of DataGrid Grouping online though).

This class could probably also be simplified to use the Transaction object too, and use ParentTransaction.X in the data bindings.

public class TransactionDetail
{
    // all properties related to transaction detail record
    public int TransactionDetailId { get; set; }
    public decimal? Monto { get; set; }
    public TipoTransaccion TipoTransaccion { get; set; }
    public string Codigo { get; set; }
    public string Nombre { get; set; }  

    // all properties related to Transaction
    public Transaction ParentTransaction { get; set; }
}

Upvotes: 1

Related Questions