0

我有以下数据网格:

<DataGrid Name="dataGridWorkOrders" ItemsSource="{StaticResource workorders}"  Grid.Row="1" IsReadOnly="True" HorizontalAlignment="Stretch" AutoGenerateColumns="False" VerticalAlignment="Top">
            <DataGrid.Columns>
 <DataGridTextColumn Header="Total Quantity" Binding="{Binding TotalQuantity}"/>
            </DataGrid.Columns>


  <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <Border BorderThickness="0" Background="BlanchedAlmond" Padding="10">
                        <DataGrid IsReadOnly="True" ItemsSource="{Binding ScheduleCollection}" AutoGenerateColumns="False">
<DataGrid.Columns>


  <DataGridTextColumn Header="Total Quantity"  Binding="{Binding Path=TotalQuantity, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>

</DataGrid.Columns>

现在在 rowdetails 模板中,我尝试访问绑定到父数据网格行的 TotalQuantity。我尝试使用祖先但不工作

编辑
模型代码:

class WorkOrders : ObservableCollection<WorkOrder>
    {
        public WorkOrders()
        {
            var orderList = OrderDetailsProvider.GetOrders() as List<WorkOrder>;

            orderList.ForEach(
                        order => this.Add(order));
        }
    }

public class WorkOrder:BaseEntity
    {
        private string orderID;
        private int totalQuantity;
        private string status;
        private ObservableCollection<Schedule> scheduleCollection;

       ....All the fields exposed as proporties
}
4

1 回答 1

2

您可以使用DataGridRow在树上找到 2 个级别的绑定来完成此操作。
找到的第一个是 DataGrid 中的那个RowDetailsTemplate(我们不想要),第二个是当前的父行RowDetailsTemplate

通过绑定到DataContext.TotalQuantityon foundDataGridRow你应该得到你正在寻找的值。

因此,在详细信息模板的列中,您可以执行以下操作:

<DataGridTextColumn Header="Total Quantity"
    Binding="{Binding Path=DataContext.TotalQuantity, 
             RelativeSource={RelativeSource AncestorType=DataGridRow, AncestorLevel=2}}" />
于 2012-11-23T08:13:16.550 回答