我有以下数据网格:
<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
}
答案 0 :(得分:2)
您可以使用在树上找到DataGridRow
2级别的绑定来执行此操作
找到的第一个是RowDetailsTemplate
中的DataGrid中的一个(我们不想要),第二个是当前RowDetailsTemplate
的父行。
通过绑定到找到的DataContext.TotalQuantity
上的DataGridRow
,您应该获得所需的值。
因此,在详细信息模板的列中,您可以执行此操作:
<DataGridTextColumn Header="Total Quantity"
Binding="{Binding Path=DataContext.TotalQuantity,
RelativeSource={RelativeSource AncestorType=DataGridRow, AncestorLevel=2}}" />