C# 如何绑定到';硕士';DataGrid中的行RowDetailsTemplate

C# 如何绑定到';硕士';DataGrid中的行RowDetailsTemplate,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我有这片XAML <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="MainW

我有这片XAML

<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; }
}
公共类主控
{
公共字符串ID{get;set;}
公共字符串主字段1{get;set;}
公共ObservableCollection详细信息{get;set;}
}
公共类详细信息
{
公共字符串MasterID{get;set;}
公共字符串字段1{get;set;}
公共字符串字段2{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}" />

为什么不在Detail类上创建一个Master属性?您也可以在设置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}" />


是的,这是一个解决方案,但我不想这样做。这个例子不能代表我想要的。我真正想知道的是如何在XAML中进行绑定,例如,如果我想显示我可以绑定到的主机的详细信息数量:但您仍然可以使用这种方法进行绑定:True(当我键入答案时,我意识到了这一点),但在我的案例中,我无法访问Master/Detail类实现的源代码。因此,我必须在不改变它们的情况下这样做。我已经找到了解决办法。我编辑了我的问题,答案在底部。谢谢