2

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

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

有什么方法可以实现吗?

我正在使用 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>  
4

2 回答 2

0

听起来您在描述分层数据结构。(具有父/子或主/详细关系的数据)

Microsoft 在其数据模板概述中提供了有关在 WPF 中显示分层数据的指南, 具体来说,这在此处进行了介绍。

于 2011-11-25T16:51:23.720 回答
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>
于 2011-11-25T22:54:06.800 回答