2012-04-13 70 views
0

我這片XAML如何綁定到 '主' 排在DataGrid RowDetailsTemplate

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfApplication1" 
    Title="MainWindow" Height="350" Width="525" 
    x:Name="Window" 
    > 
    <Grid DataContext="{Binding ElementName=Window}"> 
    <DataGrid ItemsSource="{Binding Collection}" AutoGenerateColumns="True" CanUserAddRows="False" IsReadOnly="True"> 
     <DataGrid.RowDetailsTemplate> 
      <DataTemplate DataType="{x:Type local:Master}"> 
       <ListView ItemsSource="{Binding Details}"> 
        <ListView.View> 
         <GridView> 
          <GridViewColumn Header="MasterField1" DisplayMemberBinding="{Binding RelativeSource={????}, Path=MasterField1}" /> 
          <GridViewColumn Header="DetailsField1" DisplayMemberBinding="{Binding Field1}"/> 
          <GridViewColumn Header="DetailsField2" DisplayMemberBinding="{Binding Field2}"/> 
         </GridView> 
        </ListView.View> 
       </ListView> 
      </DataTemplate> 
     </DataGrid.RowDetailsTemplate> 
    </DataGrid> 
</Grid> 

與這些兩類:

public class Master 
{ 
    public string ID { get; set; } 
    public string MasterField1 { get; set; } 
    public ObservableCollection<Detail> Details { get; set; } 
} 

public class Detail 
{ 
    public string MasterID { get; set; } 
    public string Field1 { get; set; } 
    public string Field2 { get; set; } 
} 

現在我想完成是在RowDetailsTemplate我想要顯示當前的MasterField1,但我應該如何做這個綁定工作?所以我應該怎麼做:

<GridViewColumn Header="MasterField1" DisplayMemberBinding="{Binding RelativeSource={????}, Path=MasterField1}" /> 

爲了有MasterField1顯示?

感謝


找到了解決辦法

我能做到這一點是這樣的:

<GridViewColumn Header="MasterField1" DisplayMemberBinding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=DataContext.MasterField1}" /> 

回答

0

你爲什麼不創建的詳細信息類碩士的財產?您還可以設置它,當你設置ID:

public class Detail 
{ 
    private string _masterID; 
    public string MasterID { 
     get { 
      return _masterI; 
     } 
     set { 
      _masterID = value; 
      Master = FindMasterById(value); 
     } 
    } 
    public Master Master { get ; set ; } 
    public string Field1 { get; set; } 
    public string Field2 { get; set; } 
} 

然後,只需做綁定:

<GridViewColumn Header="MasterField1" DisplayMemberBinding="{Binding Path=Master.MasterField1}" /> 
+0

是的,這將是一個解決方案,但我不想這樣做。這個例子並不代表我想要的東西。我真正想知道的是如何在XAML中進行綁定,例如,如果我想顯示可以綁定到的主數據的詳細數量: Kwomba 2012-04-13 09:44:46

+0

但是你仍然可以這樣做: dcarneiro 2012-04-13 10:02:38

+0

真的(我重新評估當我輸入我的答案時),但我的情況下,我無法訪問Master/Detail類的實現源。所以我必須做到這一點而不改變它們。 – Kwomba 2012-04-13 10:16:17

相關問題