XAML绑定问题

时间:2015-11-13 19:32:53

标签: c# wpf xaml data-binding

我花了最后三天试图完成这项工作,我不知道该怎么做,简而言之:

 <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的主键由DetallesTransaccion列中的Transaccion_Id引用(事务详细信息),此XAML我希望所有引用DetallesTransaccion引用相同的{出现在Transaccion_Id中的{1}}现在每rowDetails只出现一个。然后我有另一个问题,也与数据绑定有关,我也无法让它工作:

Transaccion

所有[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; } } 都对SubRubros列中的表格进行了自我引用,所有parentId都有直接的父级,我无法直观地表达这一点,我尝试了所有内容, SubRubroHierarchicalDataTemplate,这是我表格中的行示例:

DataTemplate
你可以开导我吗?

修改 这是我的交易类:

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

交易明细:

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

Viewmodel :(用于测试)

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
}

两个查询都是相同但不同的入口点,试图查看是否有差异,但没有。     编辑2 我得到了这个:THIS

我想要这个:Want this

1 个答案:

答案 0 :(得分:1)

第一步是将数据解析为一组准确表示数据层次结构的对象。

根据您想要的行为或外观,有两种方法可以做到这一点。

分析到单独的TransactionTransactionDetail对象,如下所示:

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

使用List<Transactions>使用DataGrid绘制DataGridTemplateColumn以绘制TransactionDetails的列表(ItemsControl,ListBox等)。

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

或者展平对象,使每个对象包含TransactionDetailTransaction数据的完整集合,并根据TransactionId将DataGrid的分组设置为分组。

您的网格数据对象将如下所示:

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

您的XAML可能与this MSDN example类似。 (对不起,不想在这里写出来。虽然有很多关于DataGrid在线分组的例子)。

这个类也可以简化为使用Transaction对象,并在数据绑定中使用ParentTransaction.X

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