用户控制datagrid行详细信息

时间:2011-11-25 16:44:55

标签: c# wpf datagrid custom-controls rowdetails

我想在行详细信息中显示我的用户控件。

我有一个复杂的数据结构,无法显示datagrid中的所有项目(只是几个),所以我想打开另一个控件并在行详细信息中显示它。我以前创建(设计)它。我不是动态地创造它。

有什么办法可以实现吗?

我正在使用C#和WPF并开发桌面应用程序。

我希望能够像:

一样使用它

    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
        //    <StackPanel Orientation="Horizontal" Margin="20,0,0,0">
        //        <TextBlock Text="Category:" FontWeight="Bold"/>
        //        <ComboBox IsEditable="True" 
        //                  ItemsSource="{Binding Source={x:Static Data:CheckBook.Categories}}" 
        //                 Text="{Binding Category}" />
        //     </StackPanel>
        <MyControl ItemsSource="blabla">
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>  

2 个答案:

答案 0 :(得分:0)

这听起来像是在描述分层数据结构。 (包含父/子或主/细节关系的数据)

Microsoft提供了有关在Data Templating Overview中在WPF中显示分层数据的指南。具体来说,这涉及here

答案 1 :(得分:0)

实现这一目标的最简单方法是使用以下内容:

请注意,如果您只需要一个包含用户/自定义控件的列,那么使用smth可能更有意义。就像带有自定义ItemTemplate的ListBox一样,因为它更轻。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid>
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            **...your user control goes here**
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>