c# - 将网格添加到 ItemsControl 的 ItemsPanelTemplate

标签 c# .net wpf itemscontrol

我想在 WPF 中添加一个 Grid 作为 ItemsControlItemPanelTemplate

我知道有很多类似的问题。但到目前为止我所看到的所有内容都在谈论动态添加行和列,并且他们没有以很好的方式涵盖它(在我看来)。

所以我想要的可能是最简单的形式,但我不知道该怎么做。

我当然知道这是行不通的。但这只是为了展示我的想法。

<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock  Text="{Binding ArticleName}" Grid.Row="0" Grid.Column="0" />
            <TextBlock  Text="{Binding ArticleTradeName}" Grid.Row="0" Grid.Column="1" />
            <ListView Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3">
                .
                .
                .
            </ListView>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
  ----------------------------------------
  | #1 name     |  #1 trade name         |
  ---------------------------------------
  | GridView                             |
  |     Row1                             |
  |    Row2                              |
  |    .                                 |
  |    .                                 |
  |     RowN                             |  
  ----------------------------------------
  | #2 name     |  #2 trade name         |
  ----------------------------------------
  | GridView                             |
  |    Row1                              |
  |    Row2                              |
  |    .                                 |
  |    .                                 |
  |     RowN                             | 
  ----------------------------------------
  ...

最佳答案

无需更改 ItemsPanel 的默认值 (StackPanel)。只需将 Grid 放入 ItemTemplate 本身即可。像这样:

<ItemsControl>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>                
                <TextBlock  Text="{Binding ArticleName}" Grid.Row="0" Grid.Column="0" />
                <TextBlock  Text="{Binding ArticleTradeName}" Grid.Row="0" Grid.Column="1" />
                <ListView ItemsSource="{Binding Samples}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3">
                    <ListView.View>
                        <GridView>
                            .
                            .
                            .
                        <GridView>
                    </ListView.View>
                </ListView>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

关于c# - 将网格添加到 ItemsControl 的 ItemsPanelTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21438616/

相关文章:

c# - 自动展开 RadGridView 新增行的 RowDetails

c# - 从并行运行的每个任务中获取完成通知

c# - 如果结果为空,则实现可查询的实践

c# - 关闭事件中的 ShowDialog

wpf - 如何在 WPF 中跨用户控件同步动画

c# - .Net 不可用 "Extension Property"之类的功能

c# - 如何使用 lambda 表达式同时获取属性 "path"和值?

c# - 什么是 'inflated type' ?

c# - 如何在 C# 中的鼠标单击事件中的窗体中的图像上绘制文本

c# - 如果我的类实现了 IEqualityComparer<T>,我是否应该实现非泛型 GetHashCode 和 Equals?