将控件添加到作为 ListView 的 ItemsPanelTemplate 的 Grid 中的空单元格

发布于 2024-09-26 21:42:41 字数 1548 浏览 1 评论 0原文

我有一个 WPF ListView,它有一个 Grid 作为 ItemsPanelTemplate。我根据项目的属性在正确的列和行中显示我的项目。但我想在网格的空单元格中放置一些控件。

这是我的代码和 xaml 的简化版本:

在资源中:

<ItemsPanelTemplate x:Key="TheTemplate">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
    </Grid>
</ItemsPanelTemplate>

在 xaml 中:

<Controls:CustomListView ItemsSource="{Binding TheCollection}"
                            ItemsPanel="{DynamicResource TheTemplate}">
</Controls:CustomListView>

最后,在我的 CustomListView 中:

protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
    base.PrepareContainerForItemOverride(element, item);
    var viewModel = item as DomainObject;
    if (viewModel != null)
    {
        element.SetValue(Grid.ColumnProperty, 1); //here I work with a converter, but this just simplifies it for StackOverflow
        element.SetValue(Grid.RowProperty, 1);
    }
}

注意:我知道我正在转换为 DomainObject,但请耐心等待。

这会给我带来一个网格,其中的项目位于正确的行和列中。但是,如果我想在空单元格中显示某些内容,例如“null”等文本,该怎么办?

我不能将其添加到我的模板中,因为这会导致应用程序崩溃,并表示 ItemsControl 将创建必要的控件。我尝试过在代码隐藏中访问网格/模板,但不太找到如何操作。也许我不应该使用ListView?也许还有其他/更好的解决方案?

I have a WPF ListView, which has a Grid as an ItemsPanelTemplate. I display my items in the correct column and row based on a property of the item. But I would like to put some controls in the empty cells of my grid.

This is a simplified version of my code and xaml:

In the resources:

<ItemsPanelTemplate x:Key="TheTemplate">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
    </Grid>
</ItemsPanelTemplate>

In the xaml:

<Controls:CustomListView ItemsSource="{Binding TheCollection}"
                            ItemsPanel="{DynamicResource TheTemplate}">
</Controls:CustomListView>

Finally, in my CustomListView:

protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
    base.PrepareContainerForItemOverride(element, item);
    var viewModel = item as DomainObject;
    if (viewModel != null)
    {
        element.SetValue(Grid.ColumnProperty, 1); //here I work with a converter, but this just simplifies it for StackOverflow
        element.SetValue(Grid.RowProperty, 1);
    }
}

NOTE: I know I'm casting to a DomainObject, but just bear with me, please.

What this will give me, is a grid with items in the correct row and column. But what if I want to display something in the empty cells, for example some text like 'null'?

I can't just add it to my template, because that crashes the application, saying the ItemsControl will create the necessary controls. I've tried accessing the grid/template in code-behind, but can't quite find how to. Maybe I shouldn't be using the ListView? Maybe there are other/better solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病毒体 2024-10-03 21:42:42

我最终做的是:

  • 用我所有的ColumnDefinitions和RowDefinitions创建一个网格(我知道我需要多少个)
  • 在我的视图的代码隐藏中,我将我的DataContext转换为我知道的
  • 我迭代集合,对于每个项目,我创建一个新的控件并将其 DataContext 设置为该项目
  • ,然后设置 RowProperty 和 ColumnProperty 并将此控件添加到网格中
  • 我还记得添加所有这些控件的位置,因此我可以将空控件添加到网格中那里还没有。

出于性能原因,我认为我会更改以下内容:
- 不是对项目集合进行迭代,而是对网格的所有可能的单元格进行迭代,添加一个控件(始终),并在存在相应项目时设置 DataContext。

这样做的原因是性能。填满网格大约需要 2 秒,我希望它能更快一些。

What I ended up doing was:

  • Make a Grid with all my ColumnDefinitions and RowDefinitions (I know how many I need)
  • In the code-behind of my View, I cast my DataContext to what I know it will be
  • I iterate over the collection, and for each item, I create a new Control and set its DataContext to the item
  • I then set the RowProperty and ColumnProperty and add this control to the Grid
  • I also remember where I added all these controls, so I can add empty controls to the Grid where there aren't any yet.

This is what I think I will change for performance reasons:
- Instead of iteration over the collection of items, iterate over all the possible cells of the Grid, add a Control (always), and set the DataContext if there is a corresponding item.

The reason to do this is performance. It takes about 2 secondes to fill the grid, and I'd like it to be faster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文