开发者

Add controls to empty cells in a Grid that's the ItemsPanelTemplate for a ListView

开发者 https://www.devze.com 2023-01-19 16:38 出处:网络
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 c

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*" />
        </G开发者_如何转开发rid.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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消