DataGrid中的DataGrid.RowDetailsTemplate与相同的ItemsSource(当前项)

本文关键字:DataGrid ItemsSource 中的 RowDetailsTemplate | 更新日期: 2023-09-27 18:19:24

我在DataGrid. rowdetailstemplate中有一个DataGrid的问题。

我有一个ObservableCollection类,它有大约20个属性,但没有其他集合。我想把它们分开看。第一个名为"mainGrid"的DataGrid应该显示前10个属性…

然后我定义了DataGrid。RowDetailsTemplate,以在用户单击该行时显示其余部分。但这行不通。在RowDetailsTemplate

中的第二个DataGrid
    <Dgv:DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <Dgv:DataGrid x:Name="dgvRowDetails">
                <Dgv:DataGrid.Columns>
                  <Dgv:DataGridTextColumn Header="Parameter 1" 
                               Binding="{Binding Parameter1}" />
                   <Dgv:DataGridTextColumn Header="Parameter 2" 
                               Binding="{Binding Parameter2}" />
                    </Dgv:DataGrid.Columns>
                </Dgv:DataGrid>
        </DataTemplate>
    </Dgv:DataGrid.RowDetailsTemplate>

它只显示RowDetails中的标题(当我单击一行时),但不显示内容。输出选项卡没有显示任何绑定错误。

我想我也需要一个ItemsSource,但我不知道如何实现它,因为其他属性指的是当前或选定的项目。

但是这个例子很好:

    <Dgv:DataGrid.RowDetailsTemplate>
        <DataTemplate>
         <StackPanel>
          <TextBlock Text = "{Binding Parameter1}" />
          <TextBlock Text = "{Binding Parameter2}" />
         </StackPanel>
        </DataTemplate>
    </Dgv:DataGrid.RowDetailsTemplate>

但是我真的想要一个DataGrid,因为header等…

我正在使用。net 3.5

提前感谢!

DataGrid中的DataGrid.RowDetailsTemplate与相同的ItemsSource(当前项)

你不能这么做。ItemsSource期望成为IEnumerable,而您想要的是给它一个单独的项目。

这是实现它的一种简单的方法。在你的对象的类中,你绑定外部数据网格的ItemSource来定义一个新的属性,以一些IEnumerable的形式返回自己

public class MyClass
{
    public IEnumerable<MyClass> AsIEnumerable
    {
        get
        {
            yield return this;
        }
    }
}

XAML

//Inner datagrid
//Datacontext is MyClass object
<Dgv:DataGrid x:Name="dgvRowDetails" 
              AutoGenerateColumns="False" 
              ItemsSource="{Binding AsIEnumerable}">
    <Dgv:DataGrid.Columns>
       <Dgv:DataGridTextColumn Header="Parameter 1" 
                               Binding="{Binding Path=Parameter1}" />
       <Dgv:DataGridTextColumn Header="Parameter 2" 
                               Binding="{Binding Path=Parameter2}" />
    </Dgv:DataGrid.Columns>
</Dgv:DataGrid>

你说你想要标题。我的建议是使用适当的工具创建标题,而不是因为存在圆圈而将正方形塞进圆圈。

试试这个

 <Dgv:DataGrid x:Name="dgvRowDetails" ItemsSource="{Binding}">