【问题标题】:User control inside datagrid row detalis数据网格行详细信息中的用户控件
【发布时间】:2012-01-06 12:07:59
【问题描述】:

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

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

有什么方法可以实现吗?

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

【问题讨论】:

    标签: c# wpf datagrid custom-controls rowdetails


    【解决方案1】:

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

    请注意,如果您只需要一个带有用户/自定义控件的列,那么使用 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>
    

    【讨论】:

      【解决方案2】:

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

      Microsoft 在其Data Templating Overview 中提供了有关在 WPF 中显示分层数据的指导,具体而言,这在here 中有所介绍。

      【讨论】:

        猜你喜欢
        • 2019-06-06
        • 1970-01-01
        • 2011-03-06
        • 2016-11-24
        • 1970-01-01
        • 1970-01-01
        • 2012-03-29
        • 2011-03-28
        • 2014-10-17
        相关资源
        最近更新 更多