2012-11-23 49 views
0

獲取數據,我有以下數據網格:無法從祖先的Datagrid

<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模板我嘗試訪問綁定到父DataGrid行TotalQuantity。我試着用祖先achiving但不工作

編輯
代碼型號:

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

你能否也展示工作訂單資源請的代碼? –

+0

@FlorianGl,Updated wororders and WorkOrder code – Simsons

+1

將'Path = TotalQuantity'改爲'Path = DataContext.TotalQuantity' –

回答

2

您可以用綁定發現DataGridRow 2級了樹做。
找到的第一個是RowDetailsTemplate中的DataGrid中的一個(我們不需要),第二個是當前RowDetailsTemplate的父行。

通過綁定到DataContext.TotalQuantity找到DataGridRow你應該得到你正在尋找的價值。

所以在細節列模板,你可以這樣做:

<DataGridTextColumn Header="Total Quantity" 
    Binding="{Binding Path=DataContext.TotalQuantity, 
      RelativeSource={RelativeSource AncestorType=DataGridRow, AncestorLevel=2}}" /> 
+0

它也適用於'Binding =「{Binding Path = DataContext.TotalQuantity, RelativeSource = {RelativeSource AncestorType = DataGrid} 「」我想。 –

+0

是的,它會工作,但也更容易打破。如果他決定在細節模板中完全不使用DataGrid,或者由於某種原因在將來爲其設置不同的DataContext,它將不再起作用。也直接綁定到包含該值的控件,比使用一些發生繼承datacontext的中間人更有意圖揭示imo。 –