views:

7

answers:

0

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?